코딩/C언어

돈 계산

런던전통손만두 2019. 9. 17. 15:47
반응형
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
#include <stdio.h>
#include <stdlib.h>
 
void pick(int money[], int itemSize,  int* bucket, int bucketSize, int k)
{
    int i, lastIndex, smallest, item, total = 0;
 
    if (k == 0)
    {
        for (i = 0; i < bucketSize; i++)
            total += money[bucket[i]];
 
        if (total == (bucketSize * 1000))
            for (i = 0; i < bucketSize; i++)
            {
                if (bucket[i] == 0)
                    continue;
 
                printf("%d ", money[bucket[i]]);
            }
        else
            return;
 
        printf("\n");
        return;
    }
 
    lastIndex = bucketSize - k - 1;
 
    if (bucketSize == k)
        smallest = 0;
    else
        smallest = bucket[lastIndex];
 
    for (item = smallest; item < itemSize; item++)
    {
        bucket[lastIndex + 1= item;
        pick(money, itemSize, bucket, bucketSize, k - 1);
    }
}
 
int main(void)
{
    int items[] = { 01000500010000 };
    int* bucket;
    int money, n;
 
    scanf("%d"&money);
    n = money / 1000;
 
    bucket = (int *)malloc(sizeof(int* n);
    pick(items, 4, bucket, n, n);
}
cs

 

결과:

반응형