-
Notifications
You must be signed in to change notification settings - Fork 0
/
exactchange.c
85 lines (64 loc) · 1.33 KB
/
exactchange.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000000
int minimum( int a, int b ){
return (a < b ? a : b );
}
int cmpfunc(const void *va, const void *vb){
const long int *ia = va;
const long int *ib = vb;
if (*ia < *ib) return -1;
else if (*ia > *ib) return +1;
else return 0;
}
int main(){
int item;
int *coin = malloc(sizeof *coin * MAX);
int *no_coin = malloc(sizeof *no_coin * MAX);
int *sum = malloc(sizeof *sum * MAX);
int testcases;
int n,i;
//int max_item = 10000;
int j,k,l;
int denomination;
int temp, start, end, coins;
scanf("%d",&testcases);
no_coin[0] = 0;
sum[0]=0;
while(1){
if(testcases == 0)
break;
scanf("%d",&item);
scanf("%d",&n);
i=0;
while(1){
if(i == n)
break;
scanf("%d",&coin[i]);
i++;
}
qsort(coin, n, sizeof (int), cmpfunc);
/*
int dp[30001];
dp[0] = 0;
for (int i=1; i<=30000; i++)
dp[i] = INFINITE;
for each coin C do
for (int v = 30001 - C - 1; v >= 0; v--)
if (dp[v] < INFINITE)
dp[v+C] = min(dp[v+C], dp[v]+1);
*/
for (i=1; i<=30000; i++)
no_coin[i] = MAX;
for(i=0;i<n;i++){
start = 30000-coin[i];
for(j= start; j>=0;j--){
if(no_coin[j] < MAX)
no_coin[j+coin[i]] = minimum(no_coin[j+coin[i]], no_coin[j]+1);
}
}
printf("answer %d\n",no_coin[item]);
testcases--;
}
return 0;
}