题解 | #硬币兑换#

硬币兑换

https://www.nowcoder.com/practice/4f900b1c941c45288dba06baa006907f

import java.util.Scanner;

public class Main {
    private static int[] coins = new int[]{1, 2, 5, 10, 20, 50, 100};

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        while(in.hasNext()){
            solution(in);
        }
    }

    /**
     * 贪心
     * @param in
     */
    private static void solution(Scanner in){
        int money = in.nextInt();

        int remain = money;
        int kind = 0;
        int amount;
        // 种类贪心 硬币面额由小到大
        for(int i=0; i<coins.length; i++){
            if(coins[i] <= remain){
                remain -= coins[i];
                kind++;
            }
        }

        // 数量贪心 剩下全选1元硬币
        amount = kind + remain;

        System.out.println(kind+" "+amount);
    }
}

全部评论

相关推荐

七牛云头号黑子:人家是过度包装被看出来没过简历,你是包都不包啊兄弟
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务