Find out how many ways to solve a series of problems (find change)
Background:
Assume four denominations of money 1 yuan, 2 yuan, 5 yuan and 10 yuan, give me 10 yuan altogether Then you can reward me 10 yuan or 10 yuan Or 5 $1 plus 1 $5 and so on If you consider the amount and order of each reward So how many different rewards will there be in the end?
You see a problem like this and want to solve it with Java code
This scenario uses recursive calculations
Here's an implementation in Java code
Consider the amount and order of each reward
public class Allprobability { public static void main(String [] args){ //Set amount int a = 10; get(a,""); System.out.println("over"); } private static int m = 0; /** * Calculate possible future probabilities * @param num Surplus * @param s Buffer String */ private static void get(int num ,String s){ //0 Direct Output if(num == 0) System.out.println("No."+(++m)+"Method"+s); //1 if(num>=1){ get(num-1,s+" "+1); } //2 if(num>=2){ get(num-2,s+" "+2); } //5 if(num>=5){ get(num-5,s+" "+5); } //10 if(num>=10){ get(num-10,s+" "+10); } } }
The output of the above code is:
Method 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Method 2 1 1 1 1 1 1 2 Method 3 1 1 1 1 1 1 2 1 ...... 127 Method 52 1 Method 128 5 129 Method 10 over
Really don't know how much you get with the number 10
Consider duplication
I added another Info class
There are four currencies and a cached num value
import java.util.LinkedList; import java.util.List; public class Allprobability2{ /** Results**/ private static List<Info> infos = new LinkedList<>(); public static void main(String [] args){ //Set amount get(new Info(10)); System.out.println("over"); } private static int m = 0; private static void get(Info s){ int num = s.getNum(); //0 Direct Output if(s.getNum() == 0) { if (!s.hasIt())System.out.println("No."+(++m)+"Method "+s); } //1 if(s.getNum()>=1){ get(s.cp().add(1).setNum(num-1)); } //2 if(num>=2){ get(s.cp().add(2).setNum(num-2)); } //5 if(num>=5){ get(s.cp().add(5).setNum(num-5)); } //10 if(num>=10){ get(s.cp().add(10).setNum(num-10)); } } //Encapsulate the result as a result class static class Info{ public Info(int num){this.num = num;} public Info(int a1, int a2, int a5, int a10) { this.a1 = a1; this.a2 = a2; this.a5 = a5; this.a10 = a10; } int num,a1,a2,a5,a10 = 0; public boolean hasIt(){ for (Info i : infos){ if(i.equals(this))return true; } infos.add(this); return false; } public int getNum(){return this.num;} public Info setNum(int num){this.num = num;return this;} public Info cp(){ return new Info(this.a1,this.a2,this.a5,this.a10); } public Info add(int a){ if(a==1)a1++; if(a==2)a2++; if(a==5)a5++; if(a==10)a10++; return this; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Info info = (Info) o; if(!(this.a1==info.a1))return false; if(!(this.a2==info.a2))return false; if(!(this.a5==info.a5))return false; if(!(this.a10==info.a10))return false; return true; } @Override public String toString() { return"1 Yuan:"+a1+"2 yuan"+ a2+"Five yuan"+a5+"10 yuan"+a10+"individual"; } } }
This output
Method 1 Yuan: 10, 2 Yuan 0, 5 Yuan 0, 10 Yuan 0 Method 2 1 yuan: 8 yuan, 1 yuan of 2 yuan, 0 yuan of 5 yuan, 0 yuan of 10 yuan Method 3 1 yuan: 6 yuan, 2 yuan of 2 yuan, 0 of 5 yuan, 0 of 10 yuan Method 4 1 yuan: 5, 2 yuan 0, 5 yuan 1, 10 yuan 0 Method 5 1 yuan: 4 yuan, 3 yuan 2 yuan, 0 yuan 5 yuan, 0 yuan 10 yuan Method 6 1 yuan: 3 yuan, 1 yuan of 2 yuan, 1 yuan of 5 yuan, 0 yuan of 10 yuan The 7th method is yuan 1: 2, 4 of 2, 0 of 5, 0 of 10 The 8th method is Yuan 1: 1, 2 of 2, 1 of 5, 0 of 10. Method 9 1 yuan: 0, 5 of 2 yuan, 0 of 5 yuan, 0 of 10 yuan Method 10 1 yuan: 0, 0 for 2 yuan, 2 for 5 yuan, 0 for 10 yuan Method 11 1 yuan: 0, 0 for 2 yuan, 0 for 5 yuan, 1 for 10 yuan over
After considering the duplicate results, there are only 11 ways that are really much less than before