Etc.

Combinations01.java

Racle 2009. 10. 16. 10:23

가쿠로 도표 작성을 위한 조합 산출용


파일명 : Combinations01.java


public class Combinations01 {

   
    public static String rtnSum(String rtn) {
   
        String strRtn = "";
       
        int temp = 0;
        int sum = 0;
       
        for(int i = 0; i < rtn.length(); i++){
           
            temp = Integer.parseInt(rtn.substring(i, i+1));
            sum = sum + temp;
           
        }
       
        strRtn = String.valueOf(sum);
        return strRtn;
    }
   
   
    public static void comb01(String s, int cnt) {
        comb01("", s, cnt);
    }
   
    private static void comb01(String prefix, String s, int cnt) {
   
        String rtn = "";
        String strRtn = "";
       
        if (s.length() > 0) {
           
            // 조합
            rtn = prefix + s.charAt(0);
           
            // 합계
            strRtn = rtnSum(rtn);
           
            if (cnt > 0) {
                if(rtn.length() == cnt){
                    System.out.println(strRtn +":"+ rtn);
                }
            }
            else {
                System.out.println(strRtn +":"+ rtn);
            }
           
           
            comb01(prefix + s.charAt(0), s.substring(1), cnt);
            comb01(prefix, s.substring(1), cnt);
        }
    }
   
   
    /*
    int N = Integer.parseInt(args[0]);
    int cnt = Integer.parseInt(args[1]);
   
   
    cnt = 0 인 경우는 모든 경우의 수 출력
    cnt = 3 인 경우는 3자리 경우의 수만 출력
    */
    public static void main(String[] args) {
   
        int N = 9;
        int cnt = 0;
       
        String targetNumber = "123456789";
        String targetStr = targetNumber.substring(0, N);
       
        comb01(targetStr, cnt);
        System.out.println();
   
    }
}