簡體   English   中英

使用遞歸打印子字符串(void方法)

[英]Printing substrings using recursion(void method)

我想按以下順序打印子字符串:“”,“ d”,“ c”,“ cd”,“ b”,“ bd”,“ bc”,“ bcd”,“ a”,“ ad” ,“ ac”,“ acd”,“ ab”,“ abd”,“ abc”,“ abcd”

我編寫了一個代碼,該代碼將獲取字符串的第一個字符,然后從enxt字符獲取隨后的子字符串,然后對其進行調用。 該過程將一直進行到字符串為空為止。 然后,我開始將字符串和字符連接起來以形成系列。 我編寫了以下代碼,但對於最后幾種編排沒有給出正確的結果。 我在這里做錯了什么?

import java.util.Scanner;

public class PrintSubstringsWithVoid {
    public static void main(String[] args) {
        Scanner scn =new Scanner(System.in);
        String str=scn.nextLine();
        printSub(str,'\0');
    }
    public static void printSub(String str,char ch1){

        if (str.length()==0){
            StringBuilder sb=new StringBuilder();
            sb.append(ch1);
            System.out.println(sb.toString()+"");
            return;
        }
        char ch=str.charAt(0);
        String st=str.substring(1);
        printSub(st,ch1);
        StringBuilder sb1=new StringBuilder();
        if (st.length()<=1){
            sb1.append(ch);
            StringBuilder sb2=new StringBuilder();
            sb2.append(ch1);
            String stt=sb2.toString()+sb1.toString();
            System.out.println(stt);

            String st1=sb2.toString()+sb1.toString()+st;
            if (st.length()!=0) {
                System.out.println(st1);
            }
        }else{

            printSub(st,ch);
        }

    }
}

這些是我的結果:-dc cd b bd bc bcd a ad ac acd b bd bc bcd

最后4個編隊在這里'a'不見了

解決方案很簡單,就像您有一個字符串“ abcd”一樣。 因此,如果您知道“ bcd”的答案,那么通過將“ a”與所有“ bcd”的結果相加,“ abcd”的答案將是“ bcd”的答案加上字符串。

例如,“ bcd”的結果為: d, c, cd, b, bd, bc, bcd,
因此,“ abcd”的結果將是: d, c, cd, b, bd, bc, bcd, a, a+(d, c, cd, b, bd, bc, bcd)

所以解決方案是:

import java.util.ArrayList;
import java.util.Scanner;

public class PrintSubstrings {

    public static void main(String[] args) {
        Scanner scn =new Scanner(System.in);
        String str=scn.nextLine();
        ArrayList<String>ans=printSub(str);
        for(int i=0;i<ans.size();i++)
        {
            System.out.print(ans.get(i)+" ");
        }
    }
    public static ArrayList<String> printSub(String str) {

        if(str.length()==0)return new ArrayList<String>();

        ArrayList<String> fx=printSub(str.substring(1));

        int size=fx.size();
        fx.add(Character.toString(str.charAt(0)));
        for(int i=0;i<size;i++)
        {
            fx.add(str.charAt(0)+fx.get(i));
        }
        return fx;
    }
}

使用void函數:

import java.util.ArrayList;
import java.util.Scanner;

public class aa {

    public static void main(String[] args) {
        Scanner scn =new Scanner(System.in);
        String str=scn.nextLine();
       printSub(str,"");

    }
    public static void  printSub(String str,String pre) {

        if(str.length()==0){
           System.out.println(pre);
          return;
        }

         printSub(str.substring(1),pre);

         printSub(str.substring(1),pre+Character.toString(str.charAt(0)));

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM