1009 FatMouse Trade

1648545-20190708115859759-951633829.png
这道题是用贪心 题目中还说要 %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));
            }
        }
    }
}

转载于:https://www.cnblogs.com/cznczai/p/11150322.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值