-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
45 lines (40 loc) · 1.07 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
const int COINS[8] = {1, 2, 5, 10, 20, 50, 100, 200};
void change(int n, int *coins, int coinsSize, int *result){
if (n==0){
(*result)++;
return;
} else if(n<0 || coinsSize==0){
return;
}
for(int i=coinsSize-1; i>=0;i--){
int* newCoins = (int*)malloc(i*sizeof(int));
memcpy(newCoins, COINS, i*sizeof(int));
int c = 1;
while (true) {
int newN = n-(c*coins[i]);
if (newN<0){
break;
}
change(newN, newCoins, i, result);
c++;
}
}
}
int main(int argc, char *argv[]){
clock_t start, end;
double elapsed;
start = clock();
int* newCoins = (int*)malloc(8*sizeof(int));
memcpy(newCoins, COINS, 8*sizeof(int));
int result = 0;
change(200, newCoins, 8, &result);
end = clock();
elapsed = ((double)end-start)/CLOCKS_PER_SEC;
printf("result=%d (elapsed=%fs)", result, elapsed);
return EXIT_SUCCESS;
}