Notice
Recent Posts
Recent Comments
Link
«   2024/10   »
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 31
Tags
more
Archives
Today
Total
관리 메뉴

아무거나 내꺼 공부할래

[개념] 조합 구하기 / c & c++ / 제한시간 없음 본문

[c언어&c++] 알고리즘 공부/개념

[개념] 조합 구하기 / c & c++ / 제한시간 없음

mero95 2021. 2. 22. 14:48
#pragma warning(disable:4996)
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;

int n, r;
int ch[20];

void dfs(int s, int L) {
	if (L == r) {
		for (int j = 0; j < L; j++) {
			printf("%d ", ch[j]);
		}
		puts("");
	}
	else {
		for (int i = s; i < n; i++) {
			ch[L] = i;
			dfs(i + 1, L + 1);
		}
	}
}

int main() {
	scanf("%d %d", &n, &r);
	dfs(0, 0);
	return 0;
}

위 코드는 정말 간단한 nCr를 코드로 작성한 것이다.

 

여러 문제에서 조합(Combination)을 사용하는 경우가 많은데

 

문제마다 if(L==r) 에서의 코드만 잘 변형시키면 문제 없음!