简单的贪心算法
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; } }
.
时间: 2024-10-02 06:01:55