JAVA 实现人民币组成问题 !

weishijun14 2015-07-06 10:25:39
第五套人民币的主币(不包括角、分)共有以下6种面值纸币:1元,5元,10元,20元,50元,100元。计算要用这几种纸币组成金额“用户输入的金额X元”,最少需要多少纸币。(用二维数组)

求大神解答!
...全文
1350 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cc_hw 2015-07-25
  • 打赏
  • 举报
回复
用贪心的思想吧,先用100填充,再用50 填充,依次减小


public static void main(String[] args) {

		Scanner scanner=new Scanner(System.in);
		
		int[] ans=new int[6];//数组 0到5 分别 代表 100 50 20.。。。。。。
		int[] money={100,50,20,10,5,1};
		
		int sum=scanner.nextInt();// 输入金额
		
		for(int i=0;i<6;i++)//用贪心的思路  先能用100就都用100,不能用100就用50最佳,
		{
			ans[i]=(sum-sum%money[i])/money[i];
			
			sum-=ans[i]*money[i];
			
			System.out.printf("%d 需要 %d\n",money[i],ans[i]);
		}
		
	}
lliiqiang 2015-07-23
  • 打赏
  • 举报
回复
其实用递归下去就可以在最少钱的情况下最先凑出结果.
迷林 2015-07-23
  • 打赏
  • 举报
回复
为毛要写的那么复杂?看不懂,还是三楼清晰
安静的萤火虫 2015-07-20
  • 打赏
  • 举报
回复
就是嘛,直接从最大的往下%和/就好了,看了最开始几楼的好高大上。。。
stghyt 2015-07-16
  • 打赏
  • 举报
回复
引用 8 楼 tianyutaizi 的回复:
感觉你们想的都好复杂…… 金额 x x/100 x%100/50 x%100%50/20 x%100%50%20/10 ... ...
这个好清晰易懂啊。
-天宇 2015-07-16
  • 打赏
  • 举报
回复
感觉你们想的都好复杂…… 金额 x x/100 x%100/50 x%100%50/20 x%100%50%20/10 ... ...
Inhibitory 2015-07-15
  • 打赏
  • 举报
回复
引用 5 楼 zouyang2014 的回复:
[quote=引用 楼主 weishijun14 的回复:] 第五套人民币的主币(不包括角、分)共有以下6种面值纸币:1元,5元,10元,20元,50元,100元。计算要用这几种纸币组成金额“用户输入的金额X元”,最少需要多少纸币。(用二维数组) 求大神解答!
这个递归死活没看懂 (int target, int currentSum, int index) 三个形参分别表示什么 target 表示输入人民币 ,index 表示下标 , currentSum 这个代表什么?[/quote] currentSum: 每次递归计算得到的和,如果这个和与 target 相等,则递归结束
琉璃村正 2015-07-15
  • 打赏
  • 举报
回复
考虑用动态规划的思路,n[money]数组记录money值需要的最小张数,kind[I]记录纸币常量,s[I]记录纸币选择方式,动规方程从0到money枚举 if(n[I+kind[j]]>n[I]+1){n[I+kind[j]]=n[I]+1; s[I+kind[j]]=s[I]+(str)kind[j]} 优点是准度高,速度快,没有堆栈限制。缺点是只能产生一种最优解,内存消耗大。
龙西四弟 2015-07-11
  • 打赏
  • 举报
回复
引用 楼主 weishijun14 的回复:
第五套人民币的主币(不包括角、分)共有以下6种面值纸币:1元,5元,10元,20元,50元,100元。计算要用这几种纸币组成金额“用户输入的金额X元”,最少需要多少纸币。(用二维数组) 求大神解答!
这个递归死活没看懂 (int target, int currentSum, int index) 三个形参分别表示什么 target 表示输入人民币 ,index 表示下标 , currentSum 这个代表什么?
alan19931103 2015-07-11
  • 打赏
  • 举报
回复
对于计算机来说,穷举永远是可行的。
三仙半 2015-07-10
  • 打赏
  • 举报
回复
既然是求最少需要多少张纸币,那就尽量使用大面值的呗,结果应该只有一个吧?我是这样做的

public static void main(String[] args) {		
		int[] values = {100, 50, 20, 10, 5, 1};
		int input = 485;
		int[] output =new int[values.length];
		int tmp = input;
		for(int i=0; i<values.length; i++){ 
			output[i] = tmp / values[i];
			tmp %= values[i];
			if(tmp==0) break;
		}
		StringBuffer buffer = new StringBuffer(""+input+"元人民币,最少需要");
		for(int i=0; i<values.length; i++){ 
			int n = output[i];
			if(n != 0){
				buffer.append(n);
				buffer.append("张");
				buffer.append(values[i]);
				buffer.append("元,");
			}
		}
		String result = buffer.substring(0, buffer.length()-1);
		System.out.println(result);		
	}
Inhibitory 2015-07-07
  • 打赏
  • 举报
回复
递归的解用 Set 过滤一下,防止重复的解。
Inhibitory 2015-07-07
  • 打赏
  • 举报
回复
1. 最笨的方法 优点:直观 缺点:要增加新的货币单位时修改容易出错
    public static void foo(int amount) {
        for (int x100 = 0; x100 * 100 < amount; ++x100) {
            for (int x50 = 0; x50 * 50 <= amount; ++x50) {
                for (int x20 = 0; x20 * 20 <= amount; ++x20) {
                    for (int x10 = 0; x10 * 10 <= amount; ++x10) {
                        for (int x5 = 0; x5 * 5 <= amount; ++x5) {
                            for (int x1 = 0; x1 <= amount; ++x1) {
                                if (x100 * 100 + x50 * 50 + x20 * 20 + x10 * 10 + x5 * 5 + x1 == amount) {
                                    System.out.printf("100: %d, 50: %d, 20: %d, 10: %d, 5: %d, 1: %d\n", x100, x50, x20, x10, x5, x1);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
2. 递归 优点:新增货币单位只需要修改 UNITS 数组 缺点:代码复杂
import java.util.LinkedList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        calculate(36, 0, 0);
    }

    private static final int[] UNITS = {100, 50, 20, 10, 5, 1};
    private static List<String> components = new LinkedList<String>();

    public static void calculate(int target, int currentSum, int index) {
        if (target == currentSum) {
            System.out.println(components);
            return;
        }

        if (index >= UNITS.length) {
            return;
        }

        for (int s = index; s < UNITS.length; ++s) {
            for (int i = 0; i <= (target - currentSum) / UNITS[s]; ++i) {
                if (currentSum + UNITS[s] * i <= target) {
                    if (i != 0) {
                        components.add(UNITS[s] + " * " + i);
                    }

                    calculate(target, currentSum + UNITS[s] * i, s+1);

                    if (i != 0) {
                        components.remove(components.size() - 1);
                    }
                }
            }
        }
    }
}
输出:
[1 * 36]
[5 * 1, 1 * 31]
[5 * 2, 1 * 26]
[5 * 3, 1 * 21]
[5 * 4, 1 * 16]
[5 * 5, 1 * 11]
[5 * 6, 1 * 6]
[5 * 7, 1 * 1]
[1 * 36]
[10 * 1, 1 * 26]
[10 * 1, 5 * 1, 1 * 21]
[10 * 1, 5 * 2, 1 * 16]
[10 * 1, 5 * 3, 1 * 11]
[10 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, 5 * 3, 1 * 1]
[10 * 2, 1 * 16]
[10 * 3, 1 * 6]
[10 * 3, 5 * 1, 1 * 1]
[10 * 3, 1 * 6]
[1 * 36]
[5 * 1, 1 * 31]
[5 * 2, 1 * 26]
[5 * 3, 1 * 21]
[5 * 4, 1 * 16]
[5 * 5, 1 * 11]
[5 * 6, 1 * 6]
[5 * 7, 1 * 1]
[1 * 36]
[20 * 1, 1 * 16]
[20 * 1, 5 * 1, 1 * 11]
[20 * 1, 5 * 2, 1 * 6]
[20 * 1, 5 * 3, 1 * 1]
[20 * 1, 1 * 16]
[20 * 1, 10 * 1, 1 * 6]
[20 * 1, 10 * 1, 5 * 1, 1 * 1]
[20 * 1, 10 * 1, 1 * 6]
[20 * 1, 1 * 16]
[20 * 1, 5 * 1, 1 * 11]
[20 * 1, 5 * 2, 1 * 6]
[20 * 1, 5 * 3, 1 * 1]
[20 * 1, 1 * 16]
[1 * 36]
[5 * 1, 1 * 31]
[5 * 2, 1 * 26]
[5 * 3, 1 * 21]
[5 * 4, 1 * 16]
[5 * 5, 1 * 11]
[5 * 6, 1 * 6]
[5 * 7, 1 * 1]
[1 * 36]
[10 * 1, 1 * 26]
[10 * 1, 5 * 1, 1 * 21]
[10 * 1, 5 * 2, 1 * 16]
[10 * 1, 5 * 3, 1 * 11]
[10 * 1, 5 * 4, 1 * 6]
[10 * 1, 5 * 5, 1 * 1]
[10 * 1, 1 * 26]
[10 * 2, 1 * 16]
[10 * 2, 5 * 1, 1 * 11]
[10 * 2, 5 * 2, 1 * 6]
[10 * 2, 5 * 3, 1 * 1]
[10 * 2, 1 * 16]
[10 * 3, 1 * 6]
[10 * 3, 5 * 1, 1 * 1]
[10 * 3, 1 * 6]
[1 * 36]
[5 * 1, 1 * 31]
[5 * 2, 1 * 26]
[5 * 3, 1 * 21]
[5 * 4, 1 * 16]
[5 * 5, 1 * 11]
[5 * 6, 1 * 6]
[5 * 7, 1 * 1]
[1 * 36]

62,636

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧