SDJ( 수돈재 아님 ㅎ )

[C++] 15655 - N과 M (6) 본문

알고리즘/Backjoon

[C++] 15655 - N과 M (6)

ShinDongJun 2020. 1. 5. 14:15

문제 링크 : https://www.acmicpc.net/problem/15655

 

15655번: N과 M (6)

N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열 고른 수열은 오름차순이어야 한다.

www.acmicpc.net

 

 

 

 

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<algorithm>
#define endl '\n'
 
int isuse[10];    // used or not used
int arr[10];    // backtracking array
int N, M;
int store[10];    // save number
 
using namespace std;
 
void NM(int k){
 
    if(k == M)    // if k == M, arr is fulled.
    {
        for(int i = 0; i < k; ++i)    // print arr
            cout << arr[i] << ' ';
        cout << endl;
        return;                        // return
    }
 
    for(int i = 0; i < N; ++i)
    {
        if(!isuse[i] && arr[k-1< store[i]) // [*] changed
        {
            isuse[i] = 1;            // used BIT on
            arr[k] = store[i];        // arr[k] = store[i].
            /*
            store[i] == 0 means store[i] is not used.
            so if will use value of store[i], turn store[i] BIT on.
            */
            NM(k+1);
            isuse[i] = 0;            // if end of backtracking, turn used BIT off.
        }
    }
 
}
 
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> N >> M;
 
    for(int i = 0; i < N; ++i)
        cin >> store[i];
    
    sort(store, store+N);
 
    NM(0);
 
    return 0;
}

'알고리즘 > Backjoon' 카테고리의 다른 글

[C++] 15657 - N과 M (8)  (0) 2020.01.05
[C++] 15656 - N과 M (7)  (0) 2020.01.05
[C++] 15654 - N과 M (5)  (0) 2020.01.05
[C++] 15652 - N과 M (4)  (0) 2020.01.05
[C++] 15651 - N과 M (3)  (0) 2020.01.05
Comments