PAT 甲级 1070 JAVA (不同输入方式,一个ac,一个超时3个用例)

ac:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    static Comparator2 com = new Comparator2();

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] in = br.readLine().split(" ");
        int kinds = Integer.parseInt(in[0]);
        int demand = Integer.parseInt(in[1]);

        String[] w = br.readLine().split(" "), p = br.readLine().split(" ");
        double result = 0;
        List<Mooncake> total_mooncake = new ArrayList<>();

        for (int i = 0; i < kinds; i++) {
            double weight = Double.parseDouble(w[i]);
            double price = Double.parseDouble(p[i]);
            double up = price / weight;
            total_mooncake.add(new Mooncake(weight,price,up));
        }

        Collections.sort(total_mooncake, com);
        double temp = demand;

        while (temp > 0) {
            if (total_mooncake.size() == 0) {
                break;
            }
            Mooncake cur = total_mooncake.remove(0);
            if (temp - cur.weight < 0) {
                result += temp * cur.unit_price;
                temp = 0;
            } else {
                result += cur.total_price;
                temp -= cur.weight;
            }
        }
        System.out.println(String.format("%.2f", result));
    }

    private static class Mooncake {
        double weight;
        double total_price;
        double unit_price;
        Mooncake(double weight, double total_price, double unit_price) {
            this.weight = weight;
            this.total_price = total_price;
            this.unit_price = unit_price;
        }
    }

    private static class Comparator2 implements Comparator<Mooncake> {
        @Override
        public int compare(Mooncake m1, Mooncake m2) {
            if (m1.unit_price < m2.unit_price) return 1;
            else return -1;
        }
    }
}

超时:

import java.util.*;

public class Main {
    static Comparator2 com = new Comparator2();

    public static void main(String[] args)  {

        double result = 0;
        Scanner sc = new Scanner(System.in);
        int kinds = sc.nextInt();
        double demand = sc.nextDouble();
        sc.nextLine();
        List<Mooncake> total_mooncake = new ArrayList<>();
        String[] w = sc.nextLine().split(" ");
        String[] p = sc.nextLine().split(" ");

        for (int i = 0; i < kinds; i++) {
            double weight = Double.parseDouble(w[i]);
            double price = Double.parseDouble(p[i]);
            double up = price / weight;
            total_mooncake.add(new Mooncake(weight,price,up));
        }
        
        Collections.sort(total_mooncake, com);
        double temp = demand;
        
        while (temp > 0) {
            if (total_mooncake.size() == 0) {
                break;
            }
            Mooncake cur = total_mooncake.remove(0);
            if (temp - cur.weight < 0) {
                result += temp * cur.unit_price;
                temp = 0;
            } else {
                result += cur.total_price;
                temp -= cur.weight;
            }
        }
        System.out.println(String.format("%.2f", result));
    }

    private static class Mooncake {
        double weight;
        double total_price;
        double unit_price;
        Mooncake(double weight, double total_price, double unit_price) {
            this.weight = weight;
            this.total_price = total_price;
            this.unit_price = unit_price;
        }
    }

    private static class Comparator2 implements Comparator<Mooncake> {
        @Override
        public int compare(Mooncake m1, Mooncake m2) {
            if (m1.unit_price < m2.unit_price) return 1;
            else return -1;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值