-2

Solve the problem by recursion:

using three type coins include 1 yuan, 2 yuan and 5 yuan, plus to 10 yuan, how many combinations?

The following is my code :

int coinNum(int num){
   if(num>=0){
       if(num==0)
           return 1;
       else 
           return coinNum(num-5)+coinNum(num-2)+coinNum(num-1);
   }else
       return 0;
 }

int main(){
   int num=coinNum(10);
   printf("%d\n",num);//the result is 128
   system("pause");
   return 0;
}

What's the error of my recursion algorithm or what's your right code ?
question supplement :
1. (5,2,2,1) and (2,5,2,1) should be counted as 1 combination .
2. the following is my code of the enumeration algorithm .

void coin(){
  int i,j,k,count=0;
  for(i=0;i<=10;i++)
    for(j=0;j<=5;j++)
        for(k=0;k<=2;k++)
            if((i+2*j+5*k)==10){
                count++;
                printf("one yuan :%d,2 yuan :%d,5 yuan :%d\n",i,j,k);
            }

   printf("总方法数%d\n",count);//the result is 10
 }
William
  • 7
  • 3
  • 1
    What makes you think there is an error? – Scott Hunter Jan 21 '16 at 08:33
  • 3
    You are asking us what is the error? You should tell us what is the error. – bolov Jan 21 '16 at 08:36
  • 6
    You ask for combination, which means (2,2,5,1) or (2,5,2,1) should be counted as 1 combination right? In your code, you cannot handle that case, ,that is your logic error. – Pham Trung Jan 21 '16 at 08:48
  • The problem has myriad solutions on the web and quite a few duplicates here. Try [this one](http://stackoverflow.com/questions/4243831/how-to-count-possible-combination-for-coin-problem). – Prune Jan 21 '16 at 17:27
  • thanks ! i am so sorry that i cannot describe the question clearly because of the first asking question here .
    1 . I get the result of 10 by enumeration algorithm which i think is right , so i feel that the result of recursion ,128 ,is wrong . but i cannot detect what's the error of my code
    2. (5,2,2,1) and (2,5,2,1) should be counted as 1 combination .@Scott Hunter@bolov @Pham Trung
    – William Jan 23 '16 at 07:48
  • thank you very much ! I am sorry for the first asking question here.@Prune – William Jan 23 '16 at 08:30

2 Answers2

2

Your code is counting the number of permutations that add up to 10. You want combinations. That means (5,2,2,1) and (2,5,2,1) should be counted as 1 combination.

In this case, the answer should be 10: (5,5), (5,2,2,1), (5,2,1,1,1), (5,1,..1), (2,2,2,2,2), (2,2,2,2,1,1), (2,2,2,1,1,1,1), (2,2,1,..1), (2,1,..1), and (1,..1).

Try this code:

int coinNum(int num, int *coins){
  if (num == 0) return 1;
  if (num < 0 || !*coins) return 0;
  return coinNum(num - *coins, coins) + coinNum(num, coins+1);
}

int main(){
  int coins[] = {5,2,1,0}; // don't forget the 0 or the program won't end

  int num=coinNum(10,coins);
  printf("%d\n",num); // the result is 10
  system("pause");
  return 0;
}

The code above tries all combinations until the sum equals or exceeds the desired sum. Note that this is not the most efficient algorithm to solve this problem, but the most simple one. For better algorithms, you should probably look for it at Computer Science Stack Exchange.

Community
  • 1
  • 1
cozyconemotel
  • 1,121
  • 2
  • 10
  • 22
  • I get the result of 10 by enumeration -for , but cannot solve it buy recursion . you are so cool ! thank you very much for your answer and the recommendation of the book . – William Jan 23 '16 at 06:59
  • my code is counting the numbers of permutations ----yes ! I confuse permutations with combinations . – William Jan 23 '16 at 07:39
  • I'm glad it helped, but why did you confirm the answer and immediately unconfirm it.. :( – cozyconemotel Jan 24 '16 at 00:56
  • thanks , I also immediately confirm it .@cozyconemotel Jan – William Jan 28 '16 at 05:01
-1

Another simple algorithm, using idea to generate not decreasing sequence of coins.

int coinNum(int num, int min_coin) {
  if (num == 0) {
    return 1;
  } else if (num < 0) {
    return 0;
  } else {
    int res = coinNum(num - 5, 5);
    if (min_coin <= 1) {
      res += coinNum(num - 1, 1);
    }
    if (min_coin <= 2) {
      res += coinNum(num - 2, 2);
    }
    return res;
  }
}

int main(){
  int num = coinNum(10, 1);
  printf("%d\n", num);
  return 0;
}
SashaMN
  • 708
  • 5
  • 16
  • yes , you are right ! so cool , thank you very much ! I may need to understand the meaning of recursion deeply . Always , i fail to find recursion algorithm to solve these problem , do you have some advice such as some books and its like? – William Jan 23 '16 at 07:17