본문 바로가기
algorithm/ACMICPC

N과 M (7) - 15656

by 에어컨조아 2019. 11. 14.

문제 : 

https://www.acmicpc.net/problem/15656

 

15656번: N과 M (7)

N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열 같은 수를 여러 번 골라도 된다.

www.acmicpc.net

 

코드 : 

#include <iostream>
#include <algorithm>

using namespace std;

int num[10];
int num[10];

void solution(int index, int n , int m)
{
	//탈출조건
	if (index == m)
	{
		for (int i = 0; i < m; i++)
		{
			cout << num[i] << " ";
		}
		cout << "\n";
		return;
	}

	for (int i = 0; i < n; i++)
	{
		num[index] = num[i];
		solution(index + 1, n, m);
	}
}

int main()
{
	int n, m;
	cin >> n >> m;

	for (int i = 0; i < n; i++)
	{
		cin >> num[i];
	}

	sort(num, &num[n]);
	
	solution(0, n , m);
	return 0;
}

'algorithm > ACMICPC' 카테고리의 다른 글

N과 M (9) - 15663  (0) 2019.11.14
N과 M (8) - 15657  (0) 2019.11.14
N과 M (6) - 15655  (0) 2019.11.14
N과 M (5) - 15654  (0) 2019.11.14
N과 M (4) - 15652  (0) 2019.11.05

댓글