티스토리 뷰

package com.company.CodeTest;

import java.util.ArrayList;

public class MainOrc {

    static int YAK, target;
    static int[] yakList, cand, used;
    static ArrayList<String> alist;
    static void input() {
        yakList = new int[]{0, 2, -900, 2000, 5, 500}; // 약리스트
        YAK = 5;    // 약 갯수
        cand = new int[]{0,0,0,0,0,0};
        used = new int[]{0,0,0,0,0,0};
        target = 3000;
        alist = new ArrayList<>();
    }

    public static void main(String[] args) {
       input();
       YakSum(1);
        System.out.print(alist);

    }

    public static void YakSum(int X){

        //X가 초과시 동작그만
        if(X == YAK + 1){
            int answer = 0;
            String answerStr = "";
            for (int i = 1 ; i <= YAK ; i++){
                if(cand[i] == 2){
                    answer /= cand[i];
                }else if(cand[i] == 5){
                    answer *= cand[i];
                }else{
                    answer += cand[i];
                }
                answerStr += cand[i]+",";
            }

            if(answer == target) alist.add(answerStr);

        }else{
            for (int i = 1; i <= YAK; i++) {

                if(used[i] != 1){
                    used[i] = 1;
                    cand[X] = yakList[i];// X를 넣어야 한다. i 넣으면 안됨
                    YakSum(X + 1);
                    cand[i] = 0;
                    used[i] = 0;
                }
            }
        }
    }


}

모든 중복없는 경우의 수를 넣는 조합이다. 

원래 문제는 3000을 맞추는 문제인데 차마 짤방은 가져오지 못하겠다.(19금 적인 면이 있음)

2의 경우 절반이라는 뜻이며, 5의 경우 5배라는 거라 if문 처리가 필요했다. 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함