简体   繁体   中英

find Number of ways to get total of 17cents (for example) using 1,5,10,25,50 cents

Why do i get a stack overflow error the thing is im trying to solve this recursively as a start,before i start using dynamic programming .In the method coins,"a" is the array that holds the coins that will form the total i want,sum is the total i want (17 for example),and i represents he index of the array that i am at

import java.util.*;
public class dp2 {//RECURSIVE WAY THEN OPTIMIZE TO DP
  public static int coins (int [] a, int sum,int i){

    if (sum==0)
        return 1;
    else if (i==a.length){
        return 0;
    }
    else if (i>a.length&&sum<a[i]){
        return coins(a,sum,i++);
    }

    else {
        return coins(a,sum-a[i],i++)+coins(a,sum-a[i],i);
    }
  }

  public static void main (String [] args ){
    Scanner sc = new Scanner (System.in);

    while (sc.hasNext()){
        int x = sc.nextInt();
        int y=x;
        int [] a ={1,5,10,25,50};
        int w = coins(a,y,0);
        System.out.println("There are " +w+ " ways to produce "+x + " coins change.");
    }               
  }

}

You have a problem with infinity loop.

First check, what do you want to return, because actually you can return only 1 or 0. So, where is other condition? Example: i == a.length - 1 or sum < 0 ? I think, you want to return sum.

And next, if you put example 17, so where is mechanism to choose witch coins from array you can select?

And next, please change return coins(a,sum-a[i],++i)+coins(a,sum-a[i],i); , because in your code i is always 0

So, maybe is good exaple code for You:

class dp2 {//RECURSIVE WAY THEN OPTIMIZE TO DP
  public static int coins (int [] a, int sum,int i){
    if (sum==0)
        return 1;
    else if (i==a.length){
        return 0;
    }else if(i + 1 == a.length || sum < 0 || sum - a[i] < 0){ //Your break condition ?
        return sum;
    }
    else if (i>a.length&&sum<a[i]){
        return coins(a,sum,i++);
    }

    else {
        return coins(a,sum-a[i],++i)+coins(a,sum-a[i],i);
    }
  }
  public static void main (String [] args ){
    Scanner sc = new Scanner (System.in);

    while (sc.hasNext()){
        int x = sc.nextInt();
        int y=x;
        int [] a ={1,5,10,25,50};
        int w = coins(a,y,0);
        System.out.println("There are " +w+ " ways to produce "+x + " coins change.");
    }
  }
}

I added one statement to your code, at line 4:

System.out.println(sum + ", " + i);

Give the input 27, the output was:

27, 0
26, 0
25, 0
.... decreasing by one each time...
3, 0
2, 0
1, 0
0, 0
-4, 1
-9, 1
-14, 1

And then it never stops because you don't check for sum < 0 .

You should be able to figure it out from there... println : the lightest-weight debugger in the world :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM