我正在尝试使用递归方法编写一个程序,该方法将对给定的字符串“Sluggo”执行以下操作。
Please enter a string: Sluggo
Sluggo
Slugg
Slug
Slu
Sl
S
luggo
lugg
lug
lu
l
uggo
ugg
ug
u
ggo
gg
g
go
g
o
到目前为止,这是我的计划:
import java.util.Scanner;
public class RecursiveDescentParser {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter a string: ");
String s = input.next();
chopBack(s.substring(0));
chopBack(s.substring(1));
chopBack(s.substring(2));
chopBack(s.substring(3));
chopBack(s.substring(4));
chopBack(s.substring(5));
}//end of main
public static String chopBack(String str)
{
if (str.length() == 0)
return "";
else
System.out.println(str.substring(0, str.length()));
return chopBack(str.substring(0, str.length() - 1 ));
}
}
我知道这不对,因为我需要编写类似于双递归方法的东西。只是想知道从这一点开始我应该去哪里?我还编写了一个方法,可以从字符串的前面删除char。在这一点上,我不太确定如何将两者连接起来哈哈。
答案 0 :(得分:1)
太棒了,谢谢你们!
刚刚与朋友玩了一下,这是他在c#中提出的解决方案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.Write("Enter a string:");
string input = Console.ReadLine();
//input = "Sluggo";
initialLength = input.Length;
recursiveFunctionYAY(input);
}
}
static int initialLength;
static void recursiveFunctionYAY(string input)
{
if (input.Length == 0)
return;
Console.WriteLine(input);
recursiveFunctionYAY(input.Substring(0, input.Length - 1));
if (input.Length == initialLength)
{
initialLength = initialLength - 1;
recursiveFunctionYAY(input.Substring(1, input.Length - 1));
}
}
}
}
答案 1 :(得分:0)
这是:
public String run(String input) {
StringBuilder sb = new StringBuilder();
process(input, sb);
return sb.toString();
}
private void process(String input, StringBuilder sb) {
tail(input, sb);
if (input.isEmpty()) {
sb.deleteCharAt(sb.length() - 1);
return;
}
process(input.substring(1, input.length()), sb);
}
private void tail(String input, StringBuilder sb) {
if (input.isEmpty())
return;
sb.append(input).append('\n');
tail(input.substring(0, input.length() - 1), sb);
}
答案 2 :(得分:0)
import java.util.Scanner;
/ ** * @author JFVARUGH * * / public class RecursiveStringRem {
static String str ;
public static void main(String[] args) {
// TODO Auto-generated method stub
RecursiveStringRem drive = new RecursiveStringRem();
Scanner sc = new Scanner(System.in);
System.out.println("Input the String");
str = sc.nextLine();
drive.superStringCall(str);
}
public void truncateSingleString(String str){
// Base Condition
if(str.length()==1){
System.out.println(str);
return;
}
// Recursive Call
System.out.println(str);
// This will truncate the string by one position
truncateSingleString(str.substring(1));
}
public void superStringCall(String str){
//Base Condition
if(str.length()==1){
return;
}
// Recursive Call
System.out.println(str);
// This will call the previous function
truncateSingleString(str.substring(1));
// Recursive call in the current function
superStringCall(str.substring(1));
}
}
我猜这些评论是不言自明的。
这是一个编译代码。