看了蛮多答案发现基本都是用c写的,虽然c确实有时间复杂度上的优势,但是用java实现也并非不可能,除了第三题其他都可以通过,此答案全部为原创并非最优解,希望有优化可以评论指出
问题描述:要求输入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;}}}}
}
问题描述:要求输入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;}}}}
}
问题描述:要求输入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写才通过的 问题描述:要求输入N个进程(0 输入格式:程序首先输入一个正整数M(0 输出格式:输出一个字符串,为最后执行进程的进程名;若无进程运行,则输出“over”(不含双引号,所有字母皆为小写)。 样例输入1: 1 3 P1 1 P2 2 P3 3 3 样例输出1:P3 问题描述:假设系统中有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 问题描述:当内存管理采用可变分区分配方案时,要求输入多个空闲分区和进程内存请求序列,输出显示采用最佳适应分配算法分配给各个进程的分区编号。 输入格式:程序要求输入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 问题描述:在请求分页式存储管理方式中,要求输入一个对5个页面的访问序列,输出当系统分配给进程物理页框数为m个时,按照FIFO页面替换算法的缺页中断次数(假设初始时页框均为空)。 输入格式:程序要求输入3行,以回车符号作为分隔,第一行是一个整数n,表示页面访问序列中访问页面的次数;第二行是n个整数,数之间以空格作为分隔,表示页面访问序列。第三行是一个整数m,表示系统分配给进程物理页框数。 输出格式:输出一个整数,表示缺页中断次数。 样例输入1: 12 4 3 2 1 4 3 5 4 3 2 1 5 3 样例输出1: 9 问题描述:现有一个8*8的存储器,要对其空间进行分配。(下标从0开始,最后一个内存块下标为63)。现已有块号为1、7、13、23、47、59的几个内存块被占用。现操作系统要求申请N块内存空间(0 输入格式:程序要求输入一个整型数N,表示要申请分配空间的大小。 输出格式:输出为一个整型数,表示最后一个被分配空间的下标。 样例输入1: 3 样例输出1: 3 问题描述:现有一个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 问题描述:现有一个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 问题描述:现有一个内存为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 问题描述:要求输入一个柱面访问请求序列以及当前磁头所在柱面号和移动方向,输出采用电梯调度算法时移动臂响应的柱面访问序列。 输入格式:程序要求输入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.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])
题目4:进程调度4----时间片轮转
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:死锁—利用银行家算法判断系统的安全性
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:存储管理—可变分区存储管理方式的最佳适应分配算法
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]
题目7:存储管理—FIFO页面替换算法计算中断次数
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
题目8:存储管理1
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
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
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
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
题目12:驱动调度—采用电梯调度算法排列出磁盘请求响应次序
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