Fork me on GitHub

1009 FatMouse Trade


这道题是用贪心 题目中还说要 %a 好像没用 也能过

 
import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int M = sc.nextInt();
			int N = sc.nextInt();
//输入数据
			if (M != -1 && N != -1) {
				int arr[][] = new int[N][2];
				for (int i = 0; i < N; i++) {
					arr[i][0] = sc.nextInt();
					arr[i][1] = sc.nextInt();
				}
//进行排列 性价比高到低排序
				int temp1;
				int temp2;
				double temp3;
				double temp4;
				double count = 0;
				for (int i = 0; i < N; i++) {
					for (int x = 0; x < N; x++) {
						temp3 = (arr[i][0] * 1.0) / arr[i][1];
						temp4 = (arr[x][0] * 1.0) / arr[x][1];
						if (temp3 > temp4) {
							temp1 = arr[i][0];
							temp2 = arr[i][1];
							arr[i][0] = arr[x][0];
							arr[i][1] = arr[x][1];
							arr[x][0] = temp1;
							arr[x][1] = temp2;
						}
					}
				}
//统计能换到的粮食
				for (int i = 0; i < N; i++) {
					if (arr[i][1] <= M) {
						count += arr[i][0];
						M = M - arr[i][1];
					} else if (arr[i][1] >= M) {
						count += arr[i][0] * 1.0 / arr[i][1] * M;
						M = 0;
						break;
					}
				}
				System.out.println(String.format("%.3f", count));
			}
		}
	}
}
posted @ 2019-07-08 11:59  cznczai  阅读(93)  评论(0编辑  收藏  举报