It is known that there are a group of coins with different denominations, and there is no limit on the number of coins. We can find all the combinations of S, and obtain the minimum and maximum number of coins.
The smallest and largest number of coins can be obtained using the greedy method, but it may not be able to obtain an effective solution, but it can increase the speed of the conclusion, which is omitted here.
The Solution Below is more time-consuming than solving the largest and smallest coin.
static int* set;static int Min = 1<<10;static int Max = 0;void LeastCoin(int* Value, int Len, int Goal, int cur) {if(Goal == 0) {for(int i = 0; i < cur; i++){printf("%d ", set[i]);}if(cur > Max){Max = cur;}if(Min > cur){Min = cur;}printf("\n");}else{for(int i = 0; i < Len; i++){if(Goal >= Value[i]){int ok = 1;for(int j = 0; j < cur; j++){if(set[j] > Value[i]){ok = 0;break;}}if(ok){ set[cur] = Value[i]; LeastCoin(Value, Len, Goal - Value[i], cur + 1); }}}}}void WLeastCoin(int* Value, int Len, int Goal) {printf("goal: %d\n", Goal);set = new int [Len];memset(set, 0 , sizeof(int)*Len);int cur = 0;LeastCoin(Value, Len, Goal, cur);printf("Max:%d \n", Max);printf("Min:%d \n", Min);}
Primary coin Problems