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

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

 

简单的贪心算法

 

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

.

相关文章:

  • 2021-09-03
  • 2021-04-19
  • 2021-07-25
  • 2021-04-04
  • 2021-05-16
  • 2021-06-26
  • 2021-06-14
  • 2021-06-10
猜你喜欢
  • 2021-07-13
  • 2022-12-23
  • 2021-11-17
  • 2021-05-16
  • 2021-05-30
  • 2021-06-07
  • 2021-09-25
相关资源
相似解决方案