본문 바로가기

백준

5568 카드 놓기

설마 순열 구하기겠어... 했는데 정말 구해야 했습니다 ~^^

 

이전에 C언어로 조합 구하는 방법 코딩한 적 있었는데 이 코드 참고해서 만들었다. 

 

burningjeong.tistory.com/287

 

조합 수 구하기

#include int g_count = 0; void comb(int start, int *visited, int len, int depth) { if (depth == 3) { g_count++; for(int i = 0; i < len; i++) { if (visited[i]) printf("%d ", i); } printf("\n"); } els..

burningjeong.tistory.com

 

이 문제가 실버 5라고.. 믿을 수 없다. (쏘 하드..)

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cmath>

#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()

using namespace std;
using i64 = long long;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

int v[10];
bool visited[10];
vector<int> selects;
set<string> ans;
int n, m;

void	comb(int depth)
{
	if (depth == m)
	{
		string str;

		for (int i = 0; i < m; i++)
			str += to_string(selects[i]);
		
		ans.insert(str);
		return;
	}

	for (int i = 0; i < n; i++)
	{
		if (visited[i])
			continue;

		visited[i] = true;
		selects.push_back(v[i]);
		comb(depth + 1);
		visited[i] = false;
		selects.pop_back();
	}
}

int     main()
{
    scanf("%d %d", &n, &m);

	for (int i = 0; i < n; i++)
		scanf("%d", &v[i]);

	comb(0);

	cout << ans.size();
    
    return 0;
}

'백준' 카테고리의 다른 글

14465 소가 길을 건너간 이유 5  (0) 2020.09.09
1966 프린터 큐  (0) 2020.09.09
2839 설탕 배달  (0) 2020.09.08
4806 줄 세기  (0) 2020.09.08
2608 로마 숫자  (0) 2020.09.06