Give you certain denomination and each kind of denomination have one fixed count. Then, you need to write one java program to output all of possible combinations to sum one target amount. For example: You have 100 Pennies, 20 Nickels, 10 Dimes and 4 Quarters to get $1. Then, how many ways do you have, and out put all of the possibilities.
The following is the java code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package currency;
public class currency1 {
public static int[] baseCurrency = {25,10,5,1};
public static void main(String[] args)
{
combCount(100);
}
public static void combCount(int total)
{
int count = 0;
System.out.println( "Quarter Dime Nickel Penny");
for(int i = 0; i <= (total/baseCurrency[0]); i++)
{
for (int j = 0; j <= (total - i*baseCurrency[0])/baseCurrency[1]; j++ )
{
int left = total - i*baseCurrency[0] - j*baseCurrency[1];
for(int k = 0; k <= left /baseCurrency[2]; k++ )
{
int m = ( left - k*baseCurrency[2] )/baseCurrency[3];
System.out.println( " "+ i + " "+ j +" " + k + " " + m);
count++;
}
}
}
System.out.println("Count:" + count);
}
}
The Output:
Quarter Dime Nickel Penny
0 0 0 100
... ... ... ...
Count: ..
No comments:
Post a Comment