백준 6603번: 로또

danbibibi·2022년 11월 6일
0

문제

문제 바로가기> 백준 6603번: 로또

풀이

backtracking을 이용해서 풀었다. 사전 순으로 조합을 구하기 위해 변수 idx를 이용했다.

#include <iostream>
#define SIX 6
#define MAX 15
using namespace std;

int k;
int num[MAX];
bool check[MAX];
int selected[SIX];

void combination(int cnt, int idx){
    if(cnt == SIX){
        for(int i=0; i<SIX; i++) cout << selected[i] << " ";
        cout << "\n";
        return;
    }
    for(int i=idx; i<k; i++){
        if(check[i]) continue;
        check[i] = true;
        selected[cnt] = num[i];
        combination(cnt+1, i+1);
        check[i] = false;
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    while(1){
        cin >> k;
        if(k == 0) break;
        for(int i=0; i<k; i++) cin >> num[i];
        combination(0, 0);
        cout << "\n";
    }
}
profile
블로그 이전) https://danbibibi.tistory.com

0개의 댓글