Fork me on GitHub

牛客网编程练习之编程马拉松:发工资

image

image

 

简单的贪心算法

 

AC代码:

import java.util.Scanner;

/**
 * @author CC11001100
 */
public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		while(sc.hasNextInt()){
			int n = sc.nextInt();
			int ans = 0;
			while(n-->0){
				ans+=resolve(sc.nextInt());
			}
			System.out.println(ans);
		}

	}

	private static final int[] moneys = new int[]{100, 50, 20, 10, 5, 2};

	private static int resolve(int n){
		int res = 0;
		for(int i=0; i<moneys.length; i++){
			if(n>=moneys[i]){
				res+=n/moneys[i];
				n%=moneys[i];
			}
		}
		return res + n;
	}

}

 

 

参考资料: https://www.nowcoder.com/practice/395e41c81109483a9ee24d387d939b44?tpId=3&tqId=10910&tPage=1&rp=&ru=/ta/hackathon&qru=/ta/hackathon/question-ranking

.

posted @ 2017-12-12 23:53  CC11001100  阅读(254)  评论(0编辑  收藏  举报