算法编程(Java)#硬币问题

问题描述

Z国的货币系统包含面值1、4、16、64元共计4种硬币,以及面值1024元的纸币。现在小Y使用1024元的纸币购买了一件价值为N(0<N<=1024)的商品,请问最少他会收到多少硬币?

输入

输入描述:
一行,包含一个整数N
输出描述:
一行,包含一个数,表示最少收到的硬币数
示例1:
输入:200
输出:17
解释:824 = 64x12 + 16x3 + 4x2, 12 + 3 + 2 = 17

代码

public class test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        //找零总计
        int s = 1024 - n;
        int count64 = s / 64;
        int res64 = s - count64 * 64;
        int count16 = res64 / 16;
        int res16 = res64 - count16 * 16;
        int count4 = res16 / 4;
        int res4 = res16 - count4 * 4;
        int count1 = res4;
        int res = count1 + count4 + count16 + count64;
        System.out.println(res);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值