2017华为机试题--任务调度
1import java.util.ArrayList;
2import java.util.List;
3import java.util.Scanner;
4 5publicclass add3 {
6publicstaticvoid main(String[] args) {
7String line =
new Scanner(System.in).nextLine();
8String[] strTasks = line.split("\\|"
);
9List
lst =newArrayList();
10for(inti = 0; i < strTasks.length; i++) {
11String[] cs = strTasks[i].substring(1, strTasks[i].length() - 1).split("\\.");
12intid = Integer.parseInt(cs[0]);
13intp = Integer.parseInt(cs[1]);
14intstart = Integer.parseInt(cs[2]);
15intallTime = Integer.parseInt(cs[3]);
16lst.add(new Task(id, p, start, allTime));
17 }
1819List queue =newArrayList();
20for(inti = 0; i < 200; i++) {
21Task currentTask =null;
22for(Task task : lst) {
23if(task.start <= i && task.left > 0) {
24if(currentTask ==null|| task.p > currentTask.p)
25currentTask = task;
26 }
27 }
28if(currentTask ==null)
29queue.add(0);
30else {
31 queue.add(currentTask.id);
32currentTask.left--;
33 }
34 }
35// System.out.println(lst);
36// System.out.println(queue);37intcurrent = queue.get(0), cnt = 1;
38List results =newArrayList();
39for(inti = 1; i < queue.size(); i++) {
40if(queue.get(i) != current) {
41results.add(new Result(current, cnt));
42current = queue.get(i);
43cnt = 1;
44}elsecnt++;
45 }
46results.add(new Result(current, cnt));
47booleanfirst =true;
48for(Result r : results) {
49if(first) {
50 System.out.print(r);
51first =false;
52}else {
53System.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]60class Task {
61int id;
62int allTime;
63int left;
64int start;
65int p;
66Task(intid,intp,intstart,int allTime) {
67this.id = id;
68this.p = p;
69this.start = start;
70this.allTime = allTime;
71this.left = allTime;
72 }
73public String toString() {
74return"[id=" + id + ", allTime=" + allTime + ", left=" + left + ", start=" + start + ", p=" + p + "]";
75 }
76}
77class Result {
78int x, y;
79Result(intx,int y) {
80this.x = x;
81this.y = y;
82 }
83public String toString() {
84returnx + "." + y;
85 }
86}
来源: http://www.bubuko.com/infodetail-2017635.html