问题 I: 带存储管理的处理器调度4

题目描述

现有一个内存为100K的采用位示图进行页面管理的多道程序设计系统,若作业调度采用高优先级(优先数越大优先级越大)调度算法(如果遇到优先级一样且只能调入一道作业时,按照输入顺序选择调度对象。),进程调度采用非剥夺式的SJF调度算法(如果遇到运行时间相同的进程,按照输入顺序选择调度对象。)。要求输入3个进程信息,输出当三个作业同时提交进入调度时进程的运行顺序。

输入格式

程序要求输入3行,以回车符号作为分隔,每行有4个数据,以空格作为分隔。首先输入一个字符串(长度小于等于10),为进程名;第2个数据类型为整型,表示进程所需的内存空间;第3个数据类型为整型,表示进程的运行时间;第4个数据类型为整型,表示进程的优先数。

输出格式

输出1行,M个字符串,字符串之间用空格作为分隔。

输入样例 复制
P1 20 2 1
P2 60 3 2
P3 30 4 3
输出样例 复制
P2 P1 P3

 

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[][] pc = new String[3][4];
        for (String[] arr : pc) {
            arr[0] = sc.next();
            for(int i=1;i<4;i++){
            arr[i] = String.valueOf(sc.nextInt());}
        }
        List<String[]> list = new ArrayList<>();
        compare(pc);
        int count = 0;
        boolean a=!isFinish(pc);
        if(a){
        do{
            if (count < 3) {
                for (String[] arr : pc) {
                    if (!"0".equals(arr[2]) && ifenter(list, arr) && !list.contains(arr)) {
                        list.add(arr);
                        count++;
                    }
                }
            }
            list.sort(Comparator.comparingInt(p -> Integer.parseInt(p[2])));
            list.sort(Comparator.comparing(p -> p[0]));
            String[] minElement = list.stream().min(Comparator.comparing(p -> Integer.parseInt(p[2]))).orElse(null);
            if (minElement != null) {
                for (int i = 0; i < 3; i++) {
                    if (minElement[0].equals(pc[i][0])) {
                        pc[i][2] = "0";
                        System.out.print(pc[i][0] + " ");
                        list.remove(minElement);
                        break;
                    }
                }
            } 
            a=!isFinish(pc);
        }while (a);
             
        }
    }
  
    public static void compare(String[][] pc) {
        for (int i = 0; i < pc.length - 1; i++) {
            for (int j = 0; j < pc.length - 1 - i; j++) {
                boolean b=Integer.parseInt(pc[j][3]) < Integer.parseInt(pc[j + 1][3]);
                boolean c=Integer.parseInt(pc[j][3]) == Integer.parseInt(pc[j + 1][3]);
                if (b) {
                    String[] temp = pc[j];
                    pc[j] = pc[j + 1];
                    pc[j + 1] = temp;
                } else if (c) {
                    boolean ad=pc[j][0].compareTo(pc[j + 1][0]) > 0;
                    if (ad) {
                        String[] temp = pc[j];
                        pc[j] = pc[j + 1];
                        pc[j + 1] = temp;
                    }
                }
            }
        }
    }
  
    public static boolean isFinish(String[][] pc) {
        for (String[] arr : pc) {
            boolean d=!"0".equals(arr[2]);
            if (d) {
                return !d;
            }
        }
        return true;
    }
  
    public static boolean ifenter(List<String[]> list, String[] pc) {
        int time = 0;
        int max = 100+1000-1000;
        for (String[] arr : list) {
            time += Integer.parseInt(arr[1]);
        }
        return time + Integer.parseInt(pc[1]) < max+1;
    }
}