代码改变世界

求给定钱数,由1,5,10,20,50,100的排列组合种数,打印显示

  杰啦  阅读(826)  评论(0编辑  收藏  举报

最近做题目遇到一个比较有意思的题目,想了蛮久了写出来的,仅供参考

题目:输入整数金额,以及纸币的数量,求组合方式

例如money=16

  {1,5,10,20,50,100}的数量分别为{5,4,3,2,1,0}

解题思路:

  1.先求出纸币数量不限的情况下,排列组合(这里我是用迭代的方式来实现),用StringBuilder存储

  2.之后再在组合里面寻找数量小于等于给定数量的组合,打印显示

下面是我的代码实现部分

复制代码
  1   2
  3 import java.util.Scanner;
  4 
  5 /**对于给定的金额,给定的纸币数量,求一共有几种组合方式
  6  * eg:1,5,10,20,50,100
  7  *    6 5 4 3 2 1
  8  *    思路:通过迭代实现求解,难点在于怎么解决每次打印问题
  9  * @author 杰拉
 10  *
 11  */
 12 public class Finder02 {
 13     public static int cou =0;
 14     public static StringBuilder stB = new StringBuilder();
 15     public static void main(String[] args) {
 16         int[] money = {1,5,10,20,50,100};
 17         int[] count = new int[6];
 18         Scanner sc = new Scanner(System.in);
 19         int Amount = Integer.parseInt( sc.nextLine());    
 20         String[] str = sc.nextLine().split(" ");
 21         sc.close();
 22         for(int i=0;i<6;i++)
 23         {
 24             count[i] = Integer.parseInt(str[i]);
 25         }
 26         find(Amount, money,"");
 27         System.out.println(cou);
 28         res(stB, count);
 29     }
      //递归求解所有的排列组合方式
30 public static void find(int Amount,int[] now,String str) 31 {
        //存放当前剩余钱数
32 int tem = Amount; 33 for(int i=0;i<now.length;i++) 34 { 35 if(i==0) 36 { 37 for(int p=0;p<Amount;p++) 38 { 39 //System.out.print("1 "); 40 stB.append("1,"); 41 } 42 Amount = 0; 43 if(Amount==0) 44 { 45 cou++; 46 stB.append("\n"); 47 //System.out.println(); 48 } 49 } 50 else 51 { 52 Amount = tem; 53 for(int j=0;j<(int)Amount/now[i];j++) 54 { 55 str = str + now[i] + ","; 56 //System.out.print(str); 57 stB.append(str); 58 find(Amount-(j+1)*now[i],IntToInt(i-1),str);
            
59 if(j==((int)Amount/now[i]-1)) 60 { 61 int x = str.length()-(j+1)*((now[i]+"").length()+1); 62 str = str.substring(0,x); 63 } 64 } 65 } 66 } 67 } 68 //实现数组的子数组 69 public static int[] IntToInt(int x) 70 { 71 int[] money = {1,5,10,20,50,100}; 72 int[] now = new int[x+1]; 73 for (int i = 0; i < now.length; i++) { 74 now[i] = money[i]; 75 } 76 return now; 77 }
     //输出符合要求的组合  
78 public static void res(StringBuilder sb,int[] count) 79 { 80 int[] money = {1,5,10,20,50,100}; 81 String[] str = sb.toString().split("\n"); 82 83 for (int i = 0; i < str.length; i++) { 84 String[] str1 = str[i].split(","); 85 int[] mount = new int[6]; 86 boolean flag = true; 87 for (int j = 0; j < str1.length; j++) { 88 int tem = Integer.parseInt(str1[j]); 89 for (int k = 0; k < mount.length; k++) { 90 if(tem==money[k]) 91 { 92 mount[k]+=1; 93 } 94 } 95 } 96 for (int j = 0; j < mount.length; j++) { 97 if(mount[j]>count[j]) 98 { 99 flag = false; 100 } 101 } 102 if(flag) 103 { 104 System.out.println(str[i].toString()); 105 } 106 107 } 108 } 109 }
复制代码

有更好的解决方法,可以在下面评论哦0.0

 

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示