zoukankan      html  css  js  c++  java
  • Java实现蓝桥杯突击战

    突击战

    你有n个部下,每个部下需要完成一项任务。第i个部下需要你花Bi分钟交待任务,然后他会立刻独立地、
    无间断地执行Ji分钟后完成任务。你需要选择交待任务的顺序,
    使得所有任务尽早执行完毕(即最后一个执行完的任务应尽早结束)
    。注意,不能同时给两个部下交待任务,但部下们可以同时执行他们各自的任务。
    [输入]
    输入包含多组数据,每组数据的第一行为部下的个数N(1≤N≤1 000);以下N行每行两个正整数B和J(1≤B≤10 000,1≤J≤10 000),即交待任务的时间和执行任务的时间。输入结束标志为N=0。
    [输出]
    对于每组数据,输出所有任务完成的最短时间。
    [样例输入]
    3
    2 5
    3 2
    2 1
    3
    3 3
    4 4
    5 5
    0
    [样例输出]
    Case 1: 8
    Case 2: 15

    package 第六次模拟;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Scanner;
    
    public class Demo4突击战 {
    
    	public static class Persion implements Comparable<Persion> {
    		int b, j;
    
    		public Persion(int b, int j) {
    			this.b = b;
    			this.j = j;
    		}
    
    		@Override
    		public int compareTo(Persion o) {
    			if (this.j > o.j)
    				return -1;
    			else if (this.j < o.j)
    				return 1;
    
    			// TODO 自动生成的方法存根
    			return 0;
    		}
    
    	}
    
    	public static void main(String[] args) {
    		int count = 0;
    		ArrayList<Persion> list = new ArrayList<Persion>();
    		Scanner sc = new Scanner(System.in);
    		while (true) {
    
    			int n = sc.nextInt();
    			if (n == 0)
    				return;
    			for (int i = 0; i < n; i++) {
    				list.add(new Persion(sc.nextInt(), sc.nextInt()));
    			}
    			Collections.sort(list);
    			int total = 0, lasttime = 0;
    			while (list.size() != 0) {
    				Persion p = list.remove(0);
    				total += p.b;
    				lasttime = Math.max(lasttime - p.b, p.j);
    			}
    			total += lasttime;
    			System.out.println("Case " + ++count + ": " + total);
    		}
    
    	}
    }
    
    
  • 相关阅读:
    Java 位运算符和移位运算符
    String类为什么是final
    SVN server 安装 并创建用户访问
    初学JavaScript正则表达式(十一)
    初学JavaScript正则表达式(十)
    初学JavaScript正则表达式(九)
    初学JavaScript正则表达式(八)
    初学JavaScript正则表达式(七)
    初学JavaScript正则表达式(六)
    初学JavaScript正则表达式(五)
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13075356.html
Copyright © 2011-2022 走看看