์‹ค๋ฒ„2 : ์กฐํ•ฉ ๋ฌธ์ œ์ด๋‹ค.

ํ’€์ด1

๊ธฐ๋ณธ์ ์ธ ์กฐํ•ฉ ๋ฌธ์ œ์ด๋‹ค.

Code

import sys
from itertools import combinations
 
input = sys.stdin.readline
 
while True:
    input_list = list(map(int, input().split()))
    if input_list[0] == 0:
        break
    k, s = input_list[0], input_list[1:]
 
    for combi in list(combinations(s, 6)):
        for c in combi:
            print(c, end=" ")
        print()
    print()

ํ’€์ด2

๋‹จ์ˆœํ•œ ๋ฌธ์ œ์˜€๋‹ค. ์žฌ๊ท€๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ ์žฌ๊ท€๋ฅผ ํ†ตํ•ด ๋“ค์–ด๊ฐˆ ๋•Œ, ํƒ์ƒ‰ํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ๋ถ€๋ถ„์„ ๊ฑฐ๋ฅด๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. ์•„๋งˆ ๋‹ค๋ฅธ ๋ถ„๋“ค๋„ ์ž‘์„ฑํ–ˆ์„ ๊ฒƒ์ด๋‹ค.

์ด ๋ฌธ์ œ์—์„œ ๊ณ ๋ คํ•ด์•ผ ๋˜๋Š” ์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ์–ด๋–ป๊ฒŒ ์ถœ๋ ฅํ•˜๊ฒŒ ๋งŒ๋“ค ๊ฒƒ์ธ๊ฐ€?
  2. ์–ด๋Š ์ƒํ™ฉ์—์„œ ํƒ์ƒ‰์„ ํ•˜์ง€ ์•Š๊ฒŒ ๊ฐ€์ง€๋ฅผ ์น  ๊ฒƒ์ธ๊ฐ€?

์ถœ๋ ฅ ๋ฐฉ๋ฒ•

checkbox๋ผ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ๊นŠ์ด๊ฐ€ 6์ด ๋˜์—ˆ์„ ๋•Œ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜์˜€๋‹ค.

๋ฐฑํŠธ๋ž˜ํ‚น

์ด ๋ฌธ์ œ๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฑํŠธ๋ ˆํ‚น์ด์ง€๋งŒ, ์จ๋ณด๋ฉด, ํ˜„์žฌ ์œ„์น˜์—์„œ ๋‚˜๋จธ์ง€ ๊ณต์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์ง€์ˆ˜์™€ ์ง€๊ธˆ ๋ถ€ํ„ฐ ์„ ํƒํ•ด์•ผ ํ•˜๋Š” ๊ฐ€์ง€์ˆ˜๋ฅผ ๋น„๊ตํ–ˆ๋‹ค.

ํ˜„์žฌ ์œ„์น˜๋กœ ๋ถ€ํ„ฐ ๋‚จ์€ ๊ณต์˜ ๊ฐœ์ˆ˜ < ์•ž์œผ๋กœ ์„ ํƒํ•ด์•ผ ํ•˜๋Š” ๊ณต์˜ ๊ฐœ์ˆ˜

์ด์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ํƒ์ƒ‰์ด ๋ถˆ๊ฐ€๋Šฅ ํ•˜๋ฏ€๋กœ ํ•จ์ˆ˜๋ฅผ ์ฝœํ•˜์ง€ ์•Š์•˜๋‹ค.

Code

// ์‹ค๋ฒ„2 : ๋ฐฑ์ค€ 6603๋ฒˆ ๋กœ๋˜
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int K = 6;
int arr[14];
bool checkbox[14] = {0};
int N = -1;
 
void go(int start, int count){
    int restOfBallFromStart = N-(start+1);
    if (count == 6) {
        for (int i = 0; i < N; i++)
            if (checkbox[i]) cout << arr[i] << " ";
        cout << '\n';
        return;
    }
    for (int i = start+1; i < N; i++) {
        if (restOfBallFromStart < K-(count+1)) break;
        else {
            checkbox[i] = 1;
            go(i, count+1);
            checkbox[i] = 0;
        }
    }
}
 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    while (N != 0) {
        fill(&arr[0], &arr[13], 0);
        cin >> N;
        for (int i = 0; i < N; i++) {
            cin >> arr[i];
        }
        go(-1, 0);
        cout << '\n';
    }
    return 0;
}
 

Reference