Thief of Wealth

- 조합


    // print all subsets of the characters in s

    public static void comb1(String s) { comb1("", s); }


    // print all subsets of the remaining elements, with given prefix 

    private 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));

        }

    }  

   // alternate implementation

    public static void comb2(String s) { comb2("", s); }

    private static void comb2(String prefix, String s) {

        System.out.println(prefix);

        for (int i = 0; i < s.length(); i++)

            comb2(prefix + s.charAt(i), s.substring(i + 1));

    }  



- 순열


public void perm(int[] arr,int depth,int n,int k){

if(depth==k){

String tmp = "";

for(int i=0; i<k; ++i) {

tmp += arr[i];

}

vec.add( Integer.parseInt(tmp) );

return;

}

for(int i=depth;i<n;i++){

swap(arr, i, depth);

perm(arr,depth+1,n,k);

swap(arr,i,depth);

}

}

public void swap(int[] list,int i,int j){

int temp=list[i];

list[i]=list[j];

list[j]=temp;

}



'개발 > Java' 카테고리의 다른 글

Java Boolean to Int  (0) 2019.12.05
이클립스 자동완성 키 변경  (0) 2019.11.29
Java Input.txt에서 입력받기  (0) 2019.11.02
Java 환경변수 설정 (오랜만에하면 까먹음)  (0) 2019.11.02
Class.forName의 역할은 무엇일까?  (0) 2019.03.19
profile on loading

Loading...