Enter m digits (positive number, must contain 1.) represents the face value of gold, and then enter n for the total amount of money exchanged, in order to obtain the minimum number of coins.
Dynamic Planning Issues 2
The basic idea of dynamic programming is to solve the problem of solving problems, solve the sub-problem, and save the solution of these sub-problems, if the solution of these sub-problems need to be solved later in solving the larger sub-problems, we can take out the calculated solutions and eliminate the repetition. The solution for saving sub-problems can be used as a form of fill-in, such as in arrays.
The code is as follows:
ImportJava.util.Scanner; Public classjin_bin_zhao_ling { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); System.out.print ("Enter the number of coin denominations:"); intM,n; M=Sc.nextint (); System.out.println ("Enter the face value of" +m+ "coins (must contain 1)"); inta[]=New int[M]; for(inti=0;i<m;++i) a[i]=Sc.nextint (); System.out.print ("Enter Total money N:"); N=Sc.nextint (); Sc.close (); intb[]=Hui (a); N=Ge_shu (b,n); System.out.println ("The minimum number of coins is:" +n);}; Public Static intGe_shu (int[] A,intN) { intLen=a.length;/*Note that the first element of the array is 0, to filter out*/ intf[]=New int[N+1]; f[0]=0; for(intI=1;i<=n;++i)/*Core Algorithms*/ { intj=1,temp=Java.lang.Integer.MAX_VALUE; while(j<len&&i>=A[j]) {Temp=math.min (f[i-A[j]], temp); ++J; } F[i]=temp+1; } returnf[n];}; Public Static int[] Hui (intA[]) {/*sorts the input data and rejects the duplicate numbers*/ intLen=1;/*return the length of the extra one*/ for(inti=0;i<a.length;++i) {intk=i; for(intj=i+1;j<a.length;++j)if(a[j]<A[k]) k=J; if(k!=i) {inttemp=A[k]; A[K]=A[i]; A[i]=temp; } } for(inti=0;i<a.length;++i) {len++; while((i+1) <a.length&&a[i]==a[i+1])/*Filter calls repeating elements*/++i; } intb[]=New int[Len]; intk=1;/*intentionally increases the length by 1, so the preceding subscript 0 is empty. Easy to use after the subscript. */ for(inti=0;i<a.length;++i) {b[k]=A[i]; ++K; while((i+1) <a.length&&a[i]==a[i+1]) ++i; } returnb;};}
The minimum number of coins in exchange for change-dynamic programming problem 2