2017 Huawei machine questions-task scheduling, 2017 Huawei

Source: Internet
Author: User

2017 Huawei machine questions-task scheduling, 2017 Huawei

Description: Enter the ID, priority, start time, and run time of the five tasks. The scheduling process supports preemption, that is, a task with a higher priority can seize a task with a lower priority and output the first 200 seconds.

Input Description: Task Information Input Format: [task ID. excellent task level. task start time. task running duration]. Use "|" to isolate tasks. The task IDs of five tasks are 1-5. The task priority range is 0-200.

Input example: [1.80.1.10] | [2.20.11.15] | [3.50.21.10] | [4.120.31.10] | [5.100.41.10]

Output example: 0.1 | 1.10 | 2.10 | 3.10 | 4.10 | 5.10 | 2.5 | 0.144

The Code is as follows:

 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 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.