Algorithm programming (Java) # coin issue

Copyright: Author: xp_9512 Source: CSDN Copyright: This article is a blogger original article, reproduced, please attach Bowen link! Original: https://blog.csdn.net/qq_40981804/article/details/89558312

Problem Description

Z country's monetary system contains a total of four kinds of face value 1,4,16,64 yuan coins and banknotes face value of 1024 yuan. Now small Y 1024 yuan notes purchased a value of N (0 <N <= 1024) of the commodity, he will receive at least ask how much coin?

Entry

Input Description:
row contains an integer N
output Description:
line, comprising a number representing the minimum number of coins received
Example 1:
Input: 200
Output: 17
explains: 824 = 64x12 + 16x3 + 4x2 , 12 + 3 + 2 = 17

Code

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);
    }
}

Guess you like

Origin blog.csdn.net/qq_40981804/article/details/89558312