首页 > 代码库 > 2017华为机试题--任务调度

2017华为机试题--任务调度

题目描述:输入5个任务的ID,任务优先级,开始运行时间和任务运行时间,其中调度过程支持抢占,即优先级值高的任务可抢占正在运行的优先级值低的任务,最后输出前200秒,任务的调度过程

输入描述:任务信息输入格式为:[任务ID.任务优秀级.任务开始运行时间.任务运行时长];任务与任务之间使用“|”隔离;5个任务的任务ID为1-5;任务优先级范围为0-200.

输入例子:[1.80.1.10]|[2.20.11.15]|[3.50.21.10]|[4.120.31.10]|[5.100.41.10]

输出例子:0.1|1.10|2.10|3.10|4.10|5.10|2.5|0.144

代码如下:

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import java.util.Scanner;
 4 
 5 public class add3 {
 6     public static void main(String[] args) {
 7         String line = new Scanner(System.in).nextLine();
 8         String[] strTasks = line.split("\\|");
 9         List<Task> lst = new ArrayList<Task>();
10         for(int i = 0; i < strTasks.length; i++) {
11             String[] cs = strTasks[i].substring(1, strTasks[i].length() - 1).split("\\.");
12             int id = Integer.parseInt(cs[0]);
13             int p = Integer.parseInt(cs[1]);
14             int start = Integer.parseInt(cs[2]);
15             int allTime = Integer.parseInt(cs[3]);
16             lst.add(new Task(id, p, start, allTime));
17         }
18         
19         List<Integer> queue = new ArrayList<Integer>();
20         for(int i = 0; i < 200; i++) {
21             Task currentTask = null;
22             for(Task task : lst) {
23                 if(task.start <= i && task.left > 0) {
24                     if(currentTask == null || task.p > currentTask.p)
25                         currentTask = task;
26                 }
27             }
28             if(currentTask == null)
29                 queue.add(0);
30             else {
31                 queue.add(currentTask.id);
32                 currentTask.left--;
33             }
34         }
35 //        System.out.println(lst);
36 //        System.out.println(queue);
37         int current = queue.get(0), cnt = 1;
38         List<Result> results = new ArrayList<Result>();
39         for(int i = 1; i < queue.size(); i++) {
40             if(queue.get(i) != current) {
41                 results.add(new Result(current, cnt));
42                 current = queue.get(i);
43                 cnt = 1;
44             } else cnt++;
45         }
46         results.add(new Result(current, cnt));
47         boolean first = true;
48         for(Result r : results) {
49             if(first) {
50                 System.out.print(r);
51                 first = false;
52             } else {
53                 System.out.print("|" + r);
54             }
55         }
56         System.out.println();
57     }
58 }
59 // [1.80.1.10]|[2.20.11.15]|[3.50.21.10]|[4.120.31.10]|[5.100.41.10]
60 class Task {
61     int id;
62     int allTime;
63     int left;
64     int start;
65     int p;
66     Task(int id, int p, int start, int allTime) {
67         this.id = id;
68         this.p = p;
69         this.start = start;
70         this.allTime = allTime;
71         this.left = allTime;
72     }
73     public String toString() {
74         return "[id=" + id + ", allTime=" + allTime + ", left=" + left + ", start=" + start + ", p=" + p + "]";
75     }
76 }
77 class Result {
78     int x, y;
79     Result(int x, int y) {
80         this.x = x;
81         this.y = y;
82     }
83     public String toString() {
84         return x + "." + y;
85     }
86 }

 

2017华为机试题--任务调度