版权声明:作者:xp_9512 来源:CSDN 版权声明:本文为博主原创文章,转载请附上博文链接! 原文: https://blog.csdn.net/qq_40981804/article/details/89558312
问题描述
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);
}
}