湖北师范大学——操作系统实训(java版)
创始人
2024-05-11 00:34:05
0

湖北师范大学——操作系统实训(java版)

看了蛮多答案发现基本都是用c写的,虽然c确实有时间复杂度上的优势,但是用java实现也并非不可能,除了第三题其他都可以通过,此答案全部为原创并非最优解,希望有优化可以评论指出

题目1:进程调度1—静态非剥夺式优先级调度计算平均作业周转时间

问题描述:要求输入3个进程的信息,假设这些进程均是在0时刻同时到达,若进程调度采用非剥夺式静态优先级(优先数数值大的表示优先级比较高;如果遇到优先级一样,按照输入顺序执行。),计算并输出平均作业周转时间。

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

输出格式:输出结果为一个浮点数,保留到小数点后一位,为系统的平均作业周转时间。

样例输入1:

P1 1 1

P2 2 2

P3 3 3

样例输出1:

4.7

import java.util.Scanner;
//p1 10 10
//p2 100 100
//p3 100 100
//ProcessA 3 8
//ProcessB 1 5
//ProcessC 2 9
//15.7
public class First {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String line1=scanner.nextLine();String line2=scanner.nextLine();String line3 = scanner.nextLine();String[] s1 = line1.split(" ");String[] s2 = line2.split(" ");String[] s3 = line3.split(" ");String[][] process={s1,s2,s3};sortByRank(process);int end1=Integer.parseInt(process[0][2]);int end2=Integer.parseInt(process[1][2])+end1;int end3=Integer.parseInt(process[2][2])+end2;float result=(end1+end2+end3)/3f;String remain1=String.format("%.1f",result);System.out.println(remain1);}public static void sortByRank(String[][] process){String[] temp;for (int i = 0; i < process.length - 1; i++) {for (int j = 0; j < process.length-1-i; j++) {if (Integer.parseInt(process[j][1])temp=process[j];process[j]=process[j+1];process[j+1]=temp;}//优先级相等就按照先后顺序排int i1 = process[j][0].compareTo(process[j + 1][0]);if ((Integer.parseInt(process[j][2])==Integer.parseInt(process[j+1][2]))&&(i1 >0)){temp=process[j];process[j]=process[j+1];process[j+1]=temp;}}}}
}

题目2:进程调度2–最高响应比优先计算每个作业的周转时间

问题描述:要求输入3个进程的信息,按照最高响应比优先的调度算法计算并输出每个进程的周转时间。(若两个进程的响应比相同,则优先选择先进入的进程。若两个进程的响应比相同,而且进入时刻也相同,则按照输入的顺序执行,如:P4和P6的响应比相同且进入时刻也相同,如P4先输入则选择P4先执行)

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

输出格式:输出三个整数之间,整数之间用空格作为分隔,为每个进程的周转时间。

样例输入1:

P1 1 1

P2 2 2

P3 3 3

样例输出1:

1 2 4

import java.util.Scanner;
//周转时间=完成时间-提交时间(等待时间+处理时间)
//Process1 2 5
//Process2 2 2
//Process3 3 2
//9 2 3
public class Second {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String line1 = scanner.nextLine();String line2 = scanner.nextLine();String line3 = scanner.nextLine();String[] s1 = line1.split(" ");String[] s2 = line2.split(" ");//进入时刻String[] s3 = line3.split(" ");//运行时间String[][] process = {s1, s2, s3};sortByIn(process);int time = 0;//等待时间=进入时刻-timeif (Integer.parseInt(process[0][1]) > time) {time = Integer.parseInt(process[0][1]);}//前两个同时进入线程,比较响应比if (Integer.parseInt(process[0][1])==Integer.parseInt(process[1][1])){if (Integer.parseInt(process[0][2])>Integer.parseInt(process[1][2])) {String[] temp = process[0];process[0] = process[1];process[1] = temp;}}//前三个都同时到达if (Integer.parseInt(process[0][1])==Integer.parseInt(process[1][1])&&Integer.parseInt(process[2][1])==Integer.parseInt(process[1][1])){sortByRemainTime(process);}time = time + Integer.parseInt(process[0][2]);process[0][2]=String.valueOf(time-Integer.parseInt(process[0][1]));if (time > Integer.parseInt(process[1][1]) && time > Integer.parseInt(process[2][1])) {//最早到到进程完成之后,剩余的进程都到达int waitTime1 =time - Integer.parseInt(process[1][1])  ;int waitTime2 = time - Integer.parseInt(process[2][1]);double ratio1 = waitTime1 / Double.parseDouble(process[1][2]);double ratio2 = waitTime2 / Double.parseDouble(process[2][2]);if (ratio1 < ratio2) {//后一个的响应比要大,就交换顺序String[] temp = process[1];process[1] = process[2];process[2] = temp;}} else if (time < Integer.parseInt(process[1][1]) && time < Integer.parseInt(process[2][1])) {//都没到time = Integer.parseInt(process[1][1]);}time = time + Integer.parseInt(process[1][2]);process[1][2]=String.valueOf(time-Integer.parseInt(process[1][1]));if (time < Integer.parseInt(process[2][1])) {time = Integer.parseInt(process[2][1]);}time = time + Integer.parseInt(process[2][2]);process[2][2]=String.valueOf(time-Integer.parseInt(process[2][1]));sortByName(process);for (String[] strings : process) {System.out.print(strings[2]+" ");}}public static void sortByIn(String[][] process){String[] temp;for (int i = 0; i < process.length - 1; i++) {for (int j = 0; j < process.length-1-i; j++) {if (Integer.parseInt(process[j][1])>Integer.parseInt(process[j+1][1])){temp=process[j];process[j]=process[j+1];process[j+1]=temp;}//优先级相等就按照先后顺序排int i1 = process[j][0].compareTo(process[j + 1][0]);if ((Integer.parseInt(process[j][1])==Integer.parseInt(process[j+1][1]))&&(i1 >0)){temp=process[j];process[j]=process[j+1];process[j+1]=temp;}}}}public static void sortByRemainTime(String[][] process){String[] temp;for (int i = 0; i < process.length - 1; i++) {for (int j = 0; j < process.length-1-i; j++) {if (Integer.parseInt(process[j][2])>Integer.parseInt(process[j+1][2])){temp=process[j];process[j]=process[j+1];process[j+1]=temp;}//优先级相等就按照先后顺序排int i1 = process[j][0].compareTo(process[j + 1][0]);if ((Integer.parseInt(process[j][2])==Integer.parseInt(process[j+1][2]))&&(i1 >0)){temp=process[j];process[j]=process[j+1];process[j+1]=temp;}}}}public static void sortByName(String[][] process){String[] temp;for (int i = 0; i < process.length - 1; i++) {for (int j = 0; j < process.length-1-i; j++) {//优先级相等就按照先后顺序排int i1 = process[j][0].compareTo(process[j + 1][0]);if ((i1 >0)){temp=process[j];process[j]=process[j+1];process[j+1]=temp;}}}}
}

题目3:进程调度3

问题描述:要求输入N个进程(N为正整型数,0

输入格式:程序首先要求输入一个整型变量N,接下来输入为N行,以回车符号作为分隔,每行有3个数据,以空格作为分隔。首先输入一个字符串(长度小于等于10),该字符串为进程名。第2个数据类型为整型,表示进程的优先数。第3个数据类型为整型,表示进程的运行时间。

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

样例输入1:

3

P1 1 1

P2 2 2

P3 3 3

样例输出1:

P3 P2 P3 P1 P2 P3

这题用此方法写最后会超时,有一个测试案例很大,大家可以集思广益进行优化,本人最后用c写才通过的

import java.util.Scanner;
//2
//p1 3 3
//p2 1 1
//p1 p1 p1 p2
public class Third {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String result = "";int n = scanner.nextInt();String[][] process = new String[n][3];for (int i = 0; i < n; i++) {process[i][0]=scanner.next();process[i][1]=String.valueOf(scanner.nextInt());process[i][2]=String.valueOf(scanner.nextInt());}sortByRank(process);int time=0;while (Integer.parseInt(process[0][2])!=0){time=Integer.parseInt(process[0][1])-Integer.parseInt(process[1][1]);if (time==0){time=1;}for (int i = 0; i < time; i++) {result=result+process[0][0]+" ";}process[0][2]=String.valueOf(Integer.parseInt(process[0][2])-time);//减运行时间if (Integer.parseInt(process[0][2])<=0){//执行完成时process[0][1] = String.valueOf(0);//优先级为最小}else {process[0][1] = String.valueOf(Integer.parseInt(process[0][1]) - time);//减优先级}sortByRank(process);}System.out.print(result);}public static void sortByRank(String[][] process){String[] temp;for (int i = 0; i < process.length - 1; i++) {for (int j = 0; j < process.length-1-i; j++) {if (Integer.parseInt(process[j][1])temp=process[j];process[j]=process[j+1];process[j+1]=temp;}int i1 = process[j][0].compareTo(process[j + 1][0]);if ((Integer.parseInt(process[j][1])==Integer.parseInt(process[j+1][1]))&&(i1 >0)){temp=process[j];process[j]=process[j+1];process[j+1]=temp;}}}}
}

题目4:进程调度4----时间片轮转

问题描述:要求输入N个进程(00)执行的那个进程的进程名。

输入格式:程序首先输入一个正整数M(0

输出格式:输出一个字符串,为最后执行进程的进程名;若无进程运行,则输出“over”(不含双引号,所有字母皆为小写)。

样例输入1:

1

3

P1 1

P2 2

P3 3

3

样例输出1:P3

import java.util.Scanner;
//2
//3
//p1 1
//p2 2
//p3 3
//4//p3
//正确
public class Fourth {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int timeSlice=Integer.parseInt(scanner.nextLine());//时间片int n=Integer.parseInt(scanner.nextLine());//进程数String[][] process = new String[n][];for (int i = 0; i < n; i++) {process[i]=scanner.nextLine().split(" ");}int rotation = Integer.parseInt(scanner.nextLine());//轮转次数int index=0;int count=0;boolean isOver=false;for (int i = 0; i < rotation; i++) {if (Integer.parseInt(process[index][1])<=0){i--;count++;if (count>n){isOver=true;break;}}else {if (i == rotation - 1) {break;}process[index][1]=String.valueOf(Integer.parseInt(process[index][1])-timeSlice);}index=(index+1)%n;}if (isOver){System.out.print("over");}else {System.out.print(process[index][0]);}}
}

题目5:死锁—利用银行家算法判断系统的安全性

问题描述:假设系统中有A、B、C三类资源,且有四个并发进程,要求输入资源总量Resource,以及每个进程运行所需的资源总量Claim和已经分配得到的资源量Allocation,利用银行家算法判断当前状态是否为安全状态,若为安全状态则给出一个安全序列。

输入格式:程序要求输入五行,以回车符号作为分隔。第一行是三个整数,整数之间以空格作为分隔,表示A、B、C三类资源的总量。下面的四行分别表示每个进程运行所需的资源总量Claim和已经分配得到的资源量Allocation;每行有7个数据,以空格作为分隔。首先输入一个字符串(长度小于等于10),为进程名;第2、3、4个数据类型为整型,表示相应进程运行所需A、B、C三种资源总量Claim;第5、6、7个数据类型为整型,表示相应进程已经分配得到的A、B、C三种资源量Allocation。

输出格式:输出一个字符串。若当前为不安全状态则输出为”false”(不含双引号,所有字母皆为小写);若当前为安全状态则输出一个安全序列,进程名之间用空格作为分隔)。

样例输入1:

9 5 7

P1 5 3 4 2 1 3

P2 9 5 2 2 1 1

P3 3 2 2 2 2 1

P4 6 4 1 1 1 1

样例输出1:

P3 P1 P4 P2

import java.util.Scanner;
//9 5 7
//P1 5 3 4 2 1 3
//P2 9 5 2 2 1 1
//P3 3 2 2 2 2 1
//P4 6 4 1 1 1 1
//正确
public class Fifth {public static void main(String[] args) {String result="";Scanner scanner = new Scanner(System.in);String[] resource = new String[3];//ABC总资源String[][] process = new String[4][];if (scanner.hasNextLine()){resource=scanner.nextLine().split(" ");}//可用的资源int availableA=Integer.parseInt(resource[0]);int availableB=Integer.parseInt(resource[1]);int availableC=Integer.parseInt(resource[2]);int[][] need = new int[4][3];for (int i = 0; i < 4; i++) {if (scanner.hasNextLine()) {process[i] = scanner.nextLine().split(" ");}availableA-=Integer.parseInt(process[i][4]);availableB-=Integer.parseInt(process[i][5]);availableC-=Integer.parseInt(process[i][6]);need[i][0]=Integer.parseInt(process[i][1])-Integer.parseInt(process[i][4]);//Aneed[i][1]=Integer.parseInt(process[i][2])-Integer.parseInt(process[i][5]);//Bneed[i][2]=Integer.parseInt(process[i][3])-Integer.parseInt(process[i][6]);//C}for (int i = 0; i < need.length; i++) {//已经执行完的进程就跳过if ("0".equals(process[i][0])&&"0".equals(process[i][1])&&"0".equals(process[i][2])) {continue;}else {//如果可利用的资源能让当前进程执行if (need[i][0] <= availableA && need[i][1] <= availableB && need[i][2] <= availableC) {result=result+process[i][0]+" ";//释放资源availableA = availableA + Integer.parseInt(process[i][4]);availableB = availableB + Integer.parseInt(process[i][5]);availableC = availableC + Integer.parseInt(process[i][6]);//标记已经执行完成process[i][0]="0";process[i][1]="0";process[i][2]="0";need[i][0]=Integer.MAX_VALUE;need[i][1]=Integer.MAX_VALUE;need[i][2]=Integer.MAX_VALUE;i = -1;continue;}}}for (int i = 0; i < process.length; i++) {if (!"0".equals(process[i][0])&&!"0".equals(process[i][1])&&!"0".equals(process[i][2])) {result="false";}}System.out.print(result);}
}

题目6:存储管理—可变分区存储管理方式的最佳适应分配算法

问题描述:当内存管理采用可变分区分配方案时,要求输入多个空闲分区和进程内存请求序列,输出显示采用最佳适应分配算法分配给各个进程的分区编号。

输入格式:程序要求输入3行,以回车符号作为分隔,第一行是一个整数n(n>=3),表示空闲分区的数量;第二行是n个整数,依次按地址递增对应第一行n个空闲分区的存储容量,每个整型数的数值代表所对应空间的剩余存储容量。n个分区(分区按地址递增依次从1开始编号,若分区X被切割分配了,剩余部分即使为0也保留原来的分区编号X)。第三行是3个整数,两个数之间以空格作为分隔,分别表示三个进程先后依次申请的内存空间的大小。

输出格式:输出一行三个整数,整数之间用空格作为分隔,分别表示三个进程所分配的分区编号;若分配失败,则用”false”表示(不含双引号,所有字母皆为小写)。

样例输入1:

6

20 5 6 18 60 4

12 7 20

样例输出1:

4 1 5

import java.util.Scanner;
//5
//20 100 10 8 40
//60 53 42//2 false false
//正确
public class Sixth {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();//空闲分区数量int[] block = new int[n];for (int i = 0; i < n; i++) {block[i]=scanner.nextInt();}int[] apply = new int[3];int[] res = new int[3];for (int i = 0; i < apply.length; i++) {apply[i]=scanner.nextInt();}for (int i = 0; i < apply.length; i++) {int min=Integer.MAX_VALUE;boolean isOut=false;for (int j = 0; j < block.length; j++) {if (block[j]>=apply[i]){isOut=true;if (block[j]-apply[i]min=block[j]-apply[i];res[i]=j+1;}}}if (!isOut){System.out.print("false ");}else {System.out.print(res[i]+" ");block[res[i] - 1] = min;}}}
}

题目7:存储管理—FIFO页面替换算法计算中断次数

问题描述:在请求分页式存储管理方式中,要求输入一个对5个页面的访问序列,输出当系统分配给进程物理页框数为m个时,按照FIFO页面替换算法的缺页中断次数(假设初始时页框均为空)。

输入格式:程序要求输入3行,以回车符号作为分隔,第一行是一个整数n,表示页面访问序列中访问页面的次数;第二行是n个整数,数之间以空格作为分隔,表示页面访问序列。第三行是一个整数m,表示系统分配给进程物理页框数。

输出格式:输出一个整数,表示缺页中断次数。

样例输入1:

12

4 3 2 1 4 3 5 4 3 2 1 5

3

样例输出1:

9

import java.util.Iterator;
import java.util.Scanner;
//12
//4 3 2 1 4 3 5 4 3 2 1 5
//3
//正确
public class Seventh {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int result=0;int n = scanner.nextInt();int[] input = new int[n];for (int i = 0; i < n; i++) {input[i]=scanner.nextInt();}int m = scanner.nextInt();Queue queue = new Queue();for (int i = 0; i < input.length; i++) {boolean flag=false;for (Integer num : queue) {if (num==input[i]){flag=true;}}//不包含该数if (!flag){//断页增加result++;//如果队列中的数量比系统分配的页框数小if (queue.size()//直接放入队列queue.enqueue(input[i]);}else {//先出队列在入queue.dequeue();queue.enqueue(input[i]);}}}System.out.print(result);}private static class Queue implements Iterable{private Node head;private Node last;private int N;private class Node{  //内部类public T item; //链表所存元素public Node next;public Node(T item,Node next){//Node的有参构造this.item=item;this.next=next;}}public Queue(){ //队列的构造this.head=new Node(null,null); //head结点不存储元素this.last=null;this.N=0;}public boolean isEmpty(){return N==0;}public int size(){return N;}//尾插public void enqueue(T t){if(last==null){ //尾结点为nulllast=new Node(t,null);head.next=last;}else{ //尾结点不为nullNode oldlast=last;//记录旧尾结点last=new Node(t,null); //更新尾结点oldlast.next=last;//旧的尾结点指向新的尾结点}N++;}//头取public T dequeue(){if(isEmpty()){return null;}else{Node oldFirst=head.next;//旧的首结点的下一个结点head.next=oldFirst.next;//更新首结点的下一个结点N--;//因为取元素是在删除,如果删完了就需要重置last=null;if(isEmpty()){last=null;}return oldFirst.item;//先进先出, 头取}}@Overridepublic Iterator iterator() {return new MyIterator();}private class MyIterator implements Iterator{private Node n;//记录每次遍历的结点MyIterator(){this.n=head;}@Overridepublic boolean hasNext() {return n.next!=null;}@Overridepublic Object next() {n=n.next;return n.item;}}}}

题目8:存储管理1

问题描述:现有一个8*8的存储器,要对其空间进行分配。(下标从0开始,最后一个内存块下标为63)。现已有块号为1、7、13、23、47、59的几个内存块被占用。现操作系统要求申请N块内存空间(0

输入格式:程序要求输入一个整型数N,表示要申请分配空间的大小。

输出格式:输出为一个整型数,表示最后一个被分配空间的下标。

样例输入1:

3

样例输出1:

3

import java.util.Scanner;
//正确
public class Eighth {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int num = scanner.nextInt();int[] storage = new int[64];storage[1]=1;storage[7]=1;storage[13]=1;storage[23]=1;storage[47]=1;storage[59]=1;if (num>58){System.out.print("false");}else {for (int i = 0; i < num; i++) {if (storage[i]!=1){storage[i]=1;}else {num++;continue;}}System.out.print(num-1);}}
}

题目9:存储管理2

问题描述:现有一个8*8的存储器,要对其空间进行分配。(下标从0开始,最后一个内存块下标为63)。现已有块号为2、7、13、23、37、47、59、61的几个内存块被占用。要求输入需分配的进程数M(0

输入格式:程序输入分为两行,第一行要求输入一个整型数M,表示要所需分配空间的进程数,接下来的第二行输入M个整型数,每个数之间用空格隔开,表示M个进程每个进程占用的内存空间大小。

输出格式:输出为M组整型数(或"false"),每个整型数表示该进程最后一个被分配的内存空间的下标(或"false"),下标(或"false")之间用空格隔开。

样例输入1:

3

3 3 3

样例输出1:

3 6 10

import java.util.Scanner;
//3
//3 3 3
//正确
public class Ninth {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int m = scanner.nextInt();int[] need = new int[m];for (int i = 0; i < m; i++) {need[i]=scanner.nextInt();}int[] storage = new int[64];storage[2]=1;storage[7]=1;storage[13]=1;storage[23]=1;storage[37]=1;storage[47]=1;storage[59]=1;storage[61]=1;int spare=56;for (int i = 0; i < m; i++) {if (need[i]>spare){System.out.print("false ");}else {for (int j = 0; j < need[i]; j++) {if (storage[j]!=1){spare--;storage[j]=1;}else {need[i]=need[i]+1;continue;}}System.out.print((need[i]-1)+" ");}}}
}

题目10:存储管理3

问题描述:现有一个8*8的存储器,要对其已分配的空间进行分配及回收。(下标从0开始,最后一个内存块下标为63)。现已有块号为2、7、13、23、37、41、47、59、61的几个内存块被占用。要求输入需分配的进程数M(0

输入格式:程序输入分为三行,第一行是一个整型数M,表示要所需分配空间的进程数,第二行为M个整型数,每个数之间用空格隔开,表示M个进程每个进程占用的内存空间大小。第三行为需要回收的进程名pN,p为小写字母,N为正整型数。

输出格式:输出为两行,第一行为一组整型数,每个整型数表示该进程最后一个被分配的内存空间的下标,下标之间用空格隔开。第二行为一组整型数,表示被回收的进程的内存块下标,多个下标之间用空格隔开。

样例输入1:

3

3 3 3

p3

样例输出1:

3 6 10

8 9 10

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//正确
public class Tenth {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int m = scanner.nextInt();int[] need = new int[m];for (int i = 0; i < m; i++) {need[i]=scanner.nextInt();}String search = scanner.next();String regEx = "[^0-9]";Pattern p = Pattern.compile(regEx);Matcher matcher = p.matcher(search);int result = Integer.parseInt(matcher.replaceAll("").trim());int[] storage = new int[64];storage[2]=64;storage[7]=64;storage[13]=64;storage[23]=64;storage[37]=64;storage[47]=64;storage[59]=64;storage[61]=64;int spare=56;for (int i = 0; i < m; i++) {if (need[i]>spare){System.out.print("false ");}else {for (int j = 0; j < need[i]; j++) {if (storage[j]==0){spare--;storage[j]=i+1;}else {need[i]=need[i]+1;continue;}}System.out.print((need[i]-1)+" ");}}System.out.println();boolean isEmpty=true;for (int i = 0; i < 64; i++) {if (storage[i]==result){System.out.print(i+" ");isEmpty=false;}}if (isEmpty){System.out.print("false ");}}}

题目11:带存储管理的处理器调度4

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

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

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

样例输入1:

P1 20 2 1

P2 60 3 2

P3 30 4 3

样例输出1:

P2 P1 P3

import java.util.*;//1 进程名 2 所需的内存空间 3 运行时间 4 优先数
//p1 20 2 1
//p2 30 3 2
//p3 30 2 3
//p1 p3 p2
//p1 80 2 2
//p2 30 5 3
//p3 40 3 1
//321
//正确
public class Eleventh {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String[][] process = new String[3][4];for (int i = 0; i < 3; i++) {process[i][0]=scanner.next();process[i][1]=String.valueOf(scanner.nextInt());process[i][2]=String.valueOf(scanner.nextInt());process[i][3]=String.valueOf(scanner.nextInt());}//内存集合List inner = new ArrayList<>();sortByRank(process);int count=0;while (!isFinish(process)) {if (count<3) {for (int i = 0; i < process.length; i++) {if ((!"0".equals(process[i][2])) && checkInner(inner, process[i])&&!inner.contains(process[i])) {inner.add(process[i]);count++;}}}//按照最短时间排序inner.sort(new Comparator() {@Overridepublic int compare(String[] o1, String[] o2) {if ((Integer.parseInt(o1[2])-Integer.parseInt(o2[2]))==0){return o1[0].compareTo(o2[0]);}else {return Integer.parseInt(o1[2]) - Integer.parseInt(o2[2]);}}});//处理内存中的进程//每次处理一个进程然后检查别的检查是否能进入for (int i = 0; i < process.length; i++) {if (inner.get(0)[0].equals(process[i][0])) {//运行时间清理表示运行完成process[i][2] = "0";System.out.print(process[i][0] + " ");//每清理完一个线程inner.remove(0);if (inner.isEmpty()){break;}}}}}//检查是否已经完成public static boolean isFinish(String[][] process){boolean flag=true;for (String[] strings : process) {if (!"0".equals(strings[2])){flag=false;}}return flag;}//检查是否能进入内存public static boolean checkInner(List inner,String[] process){int max=100;int now=0;for (String[] strings : inner) {now+=Integer.parseInt(strings[1]);}return now+Integer.parseInt(process[1])<=max;}//根据优先级排序public static void sortByRank(String[][] process){String[] temp;for (int i = 0; i < process.length - 1; i++) {for (int j = 0; j < process.length-1-i; j++) {if (Integer.parseInt(process[j][3])temp=process[j];process[j]=process[j+1];process[j+1]=temp;}int i1 = process[j][0].compareTo(process[j + 1][0]);if ((Integer.parseInt(process[j][3])==Integer.parseInt(process[j+1][3]))&&(i1 >0)){temp=process[j];process[j]=process[j+1];process[j+1]=temp;}}}}
}

题目12:驱动调度—采用电梯调度算法排列出磁盘请求响应次序

问题描述:要求输入一个柱面访问请求序列以及当前磁头所在柱面号和移动方向,输出采用电梯调度算法时移动臂响应的柱面访问序列。

输入格式:程序要求输入3行,以回车符号作为分隔,第一行是2个整数n、m,之间用空格隔开,n表示当前磁头所在的柱面号;m表示第二行输入m个数;第二行是m个整数,数之间以空格作为分隔,表示柱面访问请求序列;第三行是数字-1或1,当为-1时表示移动臂向柱面号减小方向移动,当为1时表示移动臂向柱面号增大方向移动。

输出格式:输出m个整数,数之间以空格作为分隔,采用电梯调度算法时移动臂响应的柱面访问序列。

样例输入1:

15 10

24 38 2 110 43 36 5 11 6 180

-1

样例输出1:

11 6 5 2 24 36 38 43 110 180

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
//正确
public class Twelfth {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int now=scanner.nextInt();int total=scanner.nextInt();int num[] =new int[total];for (int i = 0; i < total; i++) {num[i]=scanner.nextInt();}int direction=scanner.nextInt();List small = new ArrayList();List big = new ArrayList();//按照大小分类for (int i = 0; i < num.length; i++) {if (num[i]>now){big.add(num[i]);}else {small.add(num[i]);}}Collections.sort(big);Collections.sort(small);Collections.reverse(small);if (direction==-1){for (Integer integer : small) {System.out.print(integer+" ");}for (Integer integer : big) {System.out.print(integer+" ");}}else {for (Integer integer : big) {System.out.print(integer+" ");}for (Integer integer : small) {System.out.print(integer+" ");}}}
}

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...