1009 -

本文介绍了一个Java实现的ACM编程挑战题解,主要内容包括通过读取输入并使用自定义类进行排序来解决资源分配问题。代码示例展示了如何处理输入数据、排序和计算最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


http://acm.hdu.edu.cn/showproblem.php?pid=1009

如下写法会导致PE

System.out.format("%.3f\n", sum);


import java.awt.image.ImageConsumer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		while (true) {
			int M = input.nextInt();
			int N = input.nextInt();
			if (M == -1 && N == -1) {
				return;
			}

			List<Room> rooms = new ArrayList<>();
			double sum = 0;

			for (int i = 0; i < N; i++) {
				Room room = new Main().new Room(input.nextInt(), input.nextInt());
				rooms.add(room);
			}

			Collections.sort(rooms);

			for (Room room : rooms) {
				if (M == 0) {
					break;
				}
				if (M >= room.getCatFood()) {
					M -= room.getCatFood();
					sum += room.getJavaBean();
				} else {
					sum += M * 1.0 / room.getCatFood() * room.getJavaBean();
					M = 0;
				}
			}
			System.out.format("%.3f", sum);
			System.out.println();
		}

	}

	class Room implements Comparable<Room> {

		private int javaBean;
		private int catFood;
		private double x;

		public Room(int j, int f) {
			this.javaBean = j;
			this.catFood = f;
			this.x = javaBean * 1.0 / catFood;
		}

		public int getJavaBean() {
			return javaBean;
		}

		public void setJavaBean(int javaBean) {
			this.javaBean = javaBean;
		}

		public int getCatFood() {
			return catFood;
		}

		public void setCatFood(int catFood) {
			this.catFood = catFood;
		}

		public double getX() {
			return x;
		}

		public void setX(double x) {
			this.x = x;
		}

		@Override
		public int compareTo(Room o) {
			return this.getX() > o.getX() ? -1 : 1;
		}

		@Override
		public String toString() {
			return String.format("%s %s %s", this.javaBean, this.catFood, this.x);
		}

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值