๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜ PS] ๋ฐฑ์ค€ 2309๋ฒˆ ์ผ๊ณฑ ๋‚œ์Ÿ์ด ์ž๋ฐ” ๋ฌธ์ œํ’€์ด

by Wonit 2021. 2. 15.

๋ฌธ์ œ

ํ•ด๋‹น ํฌ์ŠคํŒ…์€ ๋ฐฑ์ค€์˜ 2309๋ฒˆ ์ผ๊ณฑ ๋‚œ์Ÿ์ด ์˜ ์ ‘๊ทผ๊ณผ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.
์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๋จผ์ € ์ƒ๊ฐํ•ด๋ณด์ž.

๋ฌธ์ œ ์ ‘๊ทผ

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์•„์ฃผ ๋Œ€ํ‘œ์ ์ธ ์™„์ „ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋ฌธ์ œ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์™„์ „ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ž…๋ ฅ์˜ ๋ฒ”์œ„๊ฐ€ ์ถฉ๋ถ„ํžˆ ์ž‘์€ ๊ฒฝ์šฐ ํ˜น์€ ์ž…๋ ฅ์— ๋Œ€ํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์‹œ๊ฐ„ ๋ณต์žก๋„ ๋ฒ”์œ„๋ฅผ ๋งŒ์กฑํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

 

์ด ์™„์ „ ํƒ์ƒ‰์—์„œ๋„ ์—ฌ๋Ÿฌ ๊ธฐ๋ฒ•์ด ์กด์žฌํ•˜์ง€๋งŒ ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๋ธŒ๋ฃจํŠธ ํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด์„œ ํ•ด๊ฒฐํ•ด๋ณด์ž.

 

๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋‹ค ํ•ด๋ด„์œผ๋กœ ๋‹ต์„ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

 

์šฐ์„  ์ผ๊ณฑ ๋‚œ์Ÿ์ด๋“ค์˜ ํ‚ค ํ•ฉ์ด 100์ด๋ผ๊ณ  ํ–ˆ์œผ๋‹ˆ 7๋ฒˆ์˜ ์กฐ๊ฑด๊ณผ for๋ฌธ์„ ํ†ตํ•ด์„œ ๋‹ต์„ ๊ฑธ๋Ÿฌ๋‚ด๋ฉฐ ๊ตฌํ•˜๋ฉด ์ถฉ๋ถ„ํžˆ ํ’€ ์ˆ˜ ์žˆ๋‹ค.


๊ทธ๋Ÿผ ์•„๋งˆ 9! ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.


ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ ์กฐ๊ธˆ ๋” ์ƒ๊ฐํ•ด๋ณด์ž.

 

ํ˜„์žฌ ์šฐ๋ฆฌ๋Š” ์•„ํ™‰ ๋‚œ์Ÿ์ด์˜ ํ‚ค๊ฐ€ ์žˆ๋Š” ์ƒํƒœ์ด๋‹ค.

 

์ผ๊ณฑ ๋‚œ์Ÿ์ด์˜ ํ‚ค ํ•ฉ์ด 100์ด๋ผ๊ณ  ํ–ˆ๋‹ค๋ฉด ๋‚œ์Ÿ์ด๋“ค ์ค‘์— ๋‘ ๋‚œ์Ÿ์ด์˜ ํ‚ค๋งŒ ๋นผ๊ณ  ๊ทธ ๊ฐ’์ด 100์ด์–ด๋„ ์—ญ์‹œ ์œ„์˜ ๋…ผ๋ฆฌ์™€ ๊ฐ™์€ ๋…ผ๋ฆฌ๊ฐ€ ๋œ๋‹ค.

 

์ •๋‹ต ์ฝ”๋“œ

for๋ฌธ์„ ๋Œ๋ฉฐ ์ผ๊ณฑ ๋‚œ์Ÿ์ด๋ฅผ ํ•˜๋‚˜์”ฉ ์ฐพ๋Š” ์ฝ”๋“œ

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] dwarfs = new int[9];

        for (int i = 0; i < 9; i++) {
            dwarfs[i] = input.nextInt();
        }
        Arrays.sort(dwarfs);
        int[] ans = new int[7];
        for (int i = 0; i < dwarfs.length-6; i++) {
            for (int j = 0; j < dwarfs.length-5; j++) {
                for (int k = 0; k < dwarfs.length-4; k++) {
                    for (int l = 0; l < dwarfs.length-3; l++) {
                        for (int m = 0; m < dwarfs.length-2; m++) {
                            for (int n = 0; n < dwarfs.length-1; n++) {
                                for (int o = 0; o < dwarfs.length; o++) {
                                    if(dwarfs[i] + dwarfs[j] + dwarfs[k] + dwarfs[l] + dwarfs[m] + dwarfs[n] + dwarfs[o] == 100){
                                        ans[0] = dwarfs[i];
                                        ans[1] = dwarfs[j];
                                        ans[2] = dwarfs[k];
                                        ans[3] = dwarfs[l];
                                        ans[4] = dwarfs[m];
                                        ans[5] = dwarfs[n];
                                        ans[6] = dwarfs[o];
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        Arrays.sort(ans);
        for ( int iter : ans) System.out.println(iter);
    }
}

์ด๊ฑด ๋ฌด์Šจ ์ฝœ๋ฐฑ ์ง€์˜ฅ๊ฐ™์ด ์ƒ๊ฒผ๋‹ค,,

์•„ํ™‰ ๋‚œ์Ÿ์ด์˜ ํ‚ค์—์„œ ๋‘ ๋‚œ์Ÿ์ด์˜ ํ‚ค๋ฅผ ๋บ€ ์ฝ”๋“œ

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int[] height = new int[9];
        int sum = 0;
        for(int i = 0; i < 9; i++) {
            height[i] = Integer.parseInt(br.readLine());
            sum += height[i];
        }
        Arrays.sort(height);
        loop: for(int i = 0; i < 8; i++) {
            for(int j = i + 1; j < 9; j++) {
                int num = height[i] + height[j];
                if(sum - num == 100) {
                    printHeight(height, i, j);
                    break loop;
                }
            }
        }
    }

    private static void printHeight(int[] arr, int index1, int index2) {

        for(int i = 0; i < arr.length; i++) {
            if(i != index1 && i != index2) System.out.println(arr[i]);
        }
    }
}

์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋Œ“๊ธ€