1. 程式人生 > >牛客網編程練習之編程馬拉松:發工資

牛客網編程練習之編程馬拉松:發工資

心算 clas c11 width true tab scanner author 算法

技術分享圖片

技術分享圖片

簡單的貪心算法

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

.

牛客網編程練習之編程馬拉松:發工資