zoukankan      html  css  js  c++  java
  • 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));
    			}
    		}
    	}
    }
    
  • 相关阅读:
    day05 集合
    day05 判断敏感字符
    day05 None类型
    day05 字典
    day04元组
    day04列表
    HDFS配额管理(实战)
    hive数据库的哪些函数操作是否走MR
    oracle 裸设备划分 --centos6.5
    redis3.0.7集群部署手册
  • 原文地址:https://www.cnblogs.com/cznczai/p/11150322.html
Copyright © 2011-2022 走看看