Notice
Recent Posts
Recent Comments
«   2024/11   »
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
Archives
Today
Total
관리 메뉴

코린이 탈출기

[알고리즘][C++] 순열, 조합, 부분집합 본문

알고리즘 정리

[알고리즘][C++] 순열, 조합, 부분집합

명란파스타 2020. 9. 3. 03:05

- 순열

void Permutation(int now_c, int goal_c, vector<int> arr)
{
	if (now_c == goal_c)
	{
    	//순열 생성 완료
		cout << "{ ";
		for (int i = 0; i < set.size(); i++)
			cout << set[i] << " ";
		cout << "}\n";
		return;
	}
		

	for (int i = 0; i < dist.size(); i++)
	{
		if (!visited[i]) {
			set.push_back(arr[i]);
			visited[i] = 1;
			Permutation(now_c + 1, goal_c, arr);
			set.pop_back();
			visited[i] = 0;
		}
			
	}
}

 

- 조합

void Combination(int now_c, int goal_c, int prev, vector<int> arr)
{
	if (now_c == goal_c)
	{
    	//조합 생성 완료
		cout << "{ ";
		for (int i = 0; i < set.size(); i++)
			cout << set[i] << " ";
		cout << "}\n";
		return;
	}
		

	for (int i = prev + 1; i < arr.size(); i++)
	{
		set.push_back(arr[i]);
		Combination(now_c + 1, goal_c, i, arr);
		set.pop_back();
	}
}

 

- 부분집합

void Subset(int now_c, int goal_c, vector<int> arr)
{
	if (now_c == goal_c)
	{
		cout << "{ ";
		for (int i = 0; i < set.size(); i++)
			cout << set[i] << " ";
		cout << "}\n";
		return;
	}
		
	if (!visited[now_c]) {
		set.push_back(arr[now_c]);
		visited[now_c] = 1;
		Subset(now_c + 1, goal_c, arr);

		set.pop_back();
		visited[now_c] = 0;
		Subset(now_c + 1, goal_c, arr);
	}
}

'알고리즘 정리' 카테고리의 다른 글

[Python] 위상 정렬  (2) 2020.09.25
[Python] 이분 탐색  (0) 2020.09.23