Iseng2 ga berhadiah :Fungsi Kombinasi Rekursif pada “array of String”


Karena sedang pusing mengerjakan program Watermarking untuk PKL yang masih banyak “Kecoaknya”, maka dari itu refreshing sambil bikin program kombinasi N kelompok. Apabila terdapat N kelompok maka pada program akan ditampilkan semua kombinasi yang mungkin, mulai dari satu kelompok hanya satu orang sampai satu kelompok 7 orang. Outputnya seperti ini…

|Ardhi
|Ardhi|Bayek
|Ardhi|Bayek|Alfath
|Ardhi|Bayek|Alfath|Dimas
|Ardhi|Bayek|Alfath|Dimas|Edan
|Ardhi|Bayek|Alfath|Dimas|Edan|Fian
|Ardhi|Bayek|Alfath|Dimas|Edan|Fian|Gila
|Ardhi|Bayek|Alfath|Dimas|Edan|Gila
|Ardhi|Bayek|Alfath|Dimas|Fian
|Ardhi|Bayek|Alfath|Dimas|Fian|Gila
|Ardhi|Bayek|Alfath|Dimas|Gila
|Ardhi|Bayek|Alfath|Edan
|Ardhi|Bayek|Alfath|Edan|Fian
|Ardhi|Bayek|Alfath|Edan|Fian|Gila
|Ardhi|Bayek|Alfath|Edan|Gila
|Ardhi|Bayek|Alfath|Fian
|Ardhi|Bayek|Alfath|Fian|Gila
|Ardhi|Bayek|Alfath|Gila
|Ardhi|Bayek|Dimas

|Ardhi|Bayek|Dimas|Edan
|Ardhi|Bayek|Dimas|Edan|Fian
|Ardhi|Bayek|Dimas|Edan|Fian|Gila
|Ardhi|Bayek|Dimas|Edan|Gila
|Ardhi|Bayek|Dimas|Fian
|Ardhi|Bayek|Dimas|Fian|Gila
|Ardhi|Bayek|Dimas|Gila
|Ardhi|Bayek|Edan
|Ardhi|Bayek|Edan|Fian
|Ardhi|Bayek|Edan|Fian|Gila
|Ardhi|Bayek|Edan|Gila
|Ardhi|Bayek|Fian
|Ardhi|Bayek|Fian|Gila
|Ardhi|Bayek|Gila
|Ardhi|Alfath
|Ardhi|Alfath|Dimas
|Ardhi|Alfath|Dimas|Edan
|Ardhi|Alfath|Dimas|Edan|Fian
|Ardhi|Alfath|Dimas|Edan|Fian|Gila
|Ardhi|Alfath|Dimas|Edan|Gila
|Ardhi|Alfath|Dimas|Fian
|Ardhi|Alfath|Dimas|Fian|Gila
|Ardhi|Alfath|Dimas|Gila
|Ardhi|Alfath|Edan
|Ardhi|Alfath|Edan|Fian
|Ardhi|Alfath|Edan|Fian|Gila
|Ardhi|Alfath|Edan|Gila
|Ardhi|Alfath|Fian
|Ardhi|Alfath|Fian|Gila
|Ardhi|Alfath|Gila
|Ardhi|Dimas
|Ardhi|Dimas|Edan
|Ardhi|Dimas|Edan|Fian
|Ardhi|Dimas|Edan|Fian|Gila
|Ardhi|Dimas|Edan|Gila
|Ardhi|Dimas|Fian
|Ardhi|Dimas|Fian|Gila
|Ardhi|Dimas|Gila
|Ardhi|Edan
|Ardhi|Edan|Fian
|Ardhi|Edan|Fian|Gila
|Ardhi|Edan|Gila
|Ardhi|Fian
|Ardhi|Fian|Gila
|Ardhi|Gila
|Bayek
|Bayek|Alfath
|Bayek|Alfath|Dimas
|Bayek|Alfath|Dimas|Edan
|Bayek|Alfath|Dimas|Edan|Fian
|Bayek|Alfath|Dimas|Edan|Fian|Gila
|Bayek|Alfath|Dimas|Edan|Gila
|Bayek|Alfath|Dimas|Fian
|Bayek|Alfath|Dimas|Fian|Gila
|Bayek|Alfath|Dimas|Gila
|Bayek|Alfath|Edan
|Bayek|Alfath|Edan|Fian
|Bayek|Alfath|Edan|Fian|Gila
|Bayek|Alfath|Edan|Gila
|Bayek|Alfath|Fian
|Bayek|Alfath|Fian|Gila
|Bayek|Alfath|Gila
|Bayek|Dimas
|Bayek|Dimas|Edan
|Bayek|Dimas|Edan|Fian
|Bayek|Dimas|Edan|Fian|Gila
|Bayek|Dimas|Edan|Gila
|Bayek|Dimas|Fian
|Bayek|Dimas|Fian|Gila
|Bayek|Dimas|Gila
|Bayek|Edan
|Bayek|Edan|Fian
|Bayek|Edan|Fian|Gila
|Bayek|Edan|Gila
|Bayek|Fian
|Bayek|Fian|Gila
|Bayek|Gila
|Alfath
|Alfath|Dimas
|Alfath|Dimas|Edan
|Alfath|Dimas|Edan|Fian
|Alfath|Dimas|Edan|Fian|Gila
|Alfath|Dimas|Edan|Gila
|Alfath|Dimas|Fian
|Alfath|Dimas|Fian|Gila
|Alfath|Dimas|Gila
|Alfath|Edan
|Alfath|Edan|Fian
|Alfath|Edan|Fian|Gila
|Alfath|Edan|Gila
|Alfath|Fian
|Alfath|Fian|Gila
|Alfath|Gila
|Dimas
|Dimas|Edan
|Dimas|Edan|Fian
|Dimas|Edan|Fian|Gila
|Dimas|Edan|Gila
|Dimas|Fian
|Dimas|Fian|Gila
|Dimas|Gila
|Edan
|Edan|Fian
|Edan|Fian|Gila
|Edan|Gila
|Fian
|Fian|Gila
|Gila
BUILD SUCCESSFUL (total time: 1 second)
 
public class Combinations {
    public static void comb1(String s){comb1("",s);}

    public static void comb1(String prefix, String s){
        if(s.length()>0){
            System.out.println(prefix + s.charAt(0));
            comb1(prefix+s.charAt(0),s.substring(1));
            comb1(prefix, s.substring(1));
        }
    }

    public static void combArr(String []s){combArr("",s);}

    public static void combArr(String prefix, String []s){
      
        if(s.length>0){
            String tmp[] = new String[s.length-1];
            for(int x=1;x<s.length;x++){
                tmp[x-1] = s[x];
            }
            System.out.println(prefix +"|"+ s[0]);
            combArr(prefix+"|"+s[0],tmp);
            combArr(prefix, tmp);
        }
    }

    public static void main (String []args){
        int N = 7;
        String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String elements = alphabet.substring(0, N);
        String [] alpha = {"Ardhi","Bayek","Alfath","Dimas","Edan","Fian","Gila"};
        //comb1(elements);
        System.out.println();
        combArr(alpha);
    }
}
del.icio.us Tags: ,,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s