Primary coin Problems

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.