import java.text.DecimalFormat;
 import java.util.Arrays;
 import java.util.Scanner;

 /*
  * 作者:Chensx1020
  * 时间:2016-12-11
  * 功能:CPU调度算法
  *         1)先到先服务调度算法(FCFS)
  *         2)最短作业优先调度算法,非抢占式(SJF)
  *         3)优先级调度算法(PSA)
  *         4)轮转法调度算法(RR)
  *         5)最高响应比调度算法(HRR)
  *         6)最短作业优先调度算法,抢占式(SJF)
  */

 public class CPU {
     //排序方法
     public static String whichComp="FCFS";
     //时间片
     public static int timeSlice=4;

     //进程类...............................................................
     public static class MyProcess implements Comparable<MyProcess> {

         private String name;    //进程名字
         private int arrive;    //到达时间
         private int work;    //区间时间(服务时间)
         private int rWork;    //还需时间
         private int priority;    //优先级
         private int begin;    //开始时间
         private double hrr;    //响应比
         private boolean flag;    //标记该进程是否执行

         public MyProcess(String name){
             this.name = name;
             this.flag = false;
         }

         public void setArrive(int arrive){
             this.arrive = arrive;
         }
         public void setWork(int work){
             this.work = work;
         }
         public void setRwork(int rWork){
             this.rWork = rWork;
         }
         public void setPriority(int priority){
             this.priority = priority;
         }
         public void setBegin(int begin){
             this.begin = begin;
         }
         public void setHrr(){
             this.hrr = (this.begin-this.arrive)*1.0/this.work + 1;
         }
         public void setHrr(double hrr){
             this.hrr = hrr;
         }
         public void setFlag(){
             this.flag = true;
         }

         public String getName(){
             return this.name;
         }
         public int getArrive(){
             return this.arrive;
         }
         public int getWork(){
             return this.work;
         }
         public int getRwork(){
             return this.rWork;
         }
         public int getPriority(){
             return this.priority;
         }
         public int getBegin(){
             return this.begin;
         }
         public double getHrr(){
             return this.hrr;
         }
         public boolean getFlag(){
             return this.flag;
         }

         @Override
         public int compareTo(MyProcess o) {
             if(whichComp.equals("FCFS")){
                 if(this.arrive>o.arrive)
                     return 1;
                 else if(this.arrive<o.arrive)
                     return -1;
                 else
                     return this.work-o.work;
             }
             else if(whichComp.equals("SJF")){
                 if(this.work>o.work)
                     return 1;
                 else if(this.work<o.work)
                     return -1;
                 else
                     return this.arrive-o.arrive;
             }
             else if(whichComp.equals("SJFS")){
                 if(this.rWork>o.rWork)
                     return 1;
                 else if(this.rWork<o.rWork)
                     return -1;
                 else
                     return this.arrive-o.arrive;

             }
             else if(whichComp.equals("PSA")){
                 if(this.priority>o.priority)
                     return 1;
                 else if(this.priority<o.priority)
                     return -1;
                 else
                     return this.arrive-o.arrive;
             }
             else if(whichComp.equals("HRR")){
                 if(this.begin>o.begin)
                     return 1;
                 else if(this.begin<o.begin)
                     return -1;
                 else{
                     if(this.hrr < o.hrr)
                         return 1;
                     else if(this.hrr > o.hrr)
                         return -1;
                     else{
                         return this.arrive-o.arrive;
 //                        if(this.arrive>o.arrive)
 //                            return 1;
 //                        else if(this.arrive<o.arrive)
 //                            return -1;
 //                        else
 //                            return this.work-o.work;
                     }
                 }
             }
             else{
                 return 0;
             }
         }
     }

     //FCFS类...............................................................
     public static class FCFS{

         private int count;
         private String temp;

         public FCFS(MyProcess test[]){
             Arrays.sort(test);

             count = test[0].getArrive();
             temp = "";

             int length = test.length;

             int wait = 0;    //总等待时间
             int cycle = 0;    //总周转时间

             System.out.print("------------------------------------------------------------------");
             System.out.println("------------------------------------------------------------------");
             System.out.println("先到先服务调度算法的结果为:");
             System.out.println("进程名字\t到达时间\t服务时间\t开始时间\t结束时间\t等待时间\t"
                     + "周转时间\t带权周转时间\t");

             for(int i=0; i<length-1; ++i){

                 System.out.println(test[i].getName()+"\t"
                         +test[i].getArrive()+"\t"
                         +test[i].getWork()+"\t"
                         +count+"\t"
                         +(count+test[i].getWork())+"\t"
                         +(count-test[i].getArrive())+"\t"
                         +(count-test[i].getArrive()+test[i].getWork())+"\t"
                         +(count-test[i].getArrive()+test[i].getWork())*1.0/test[i].getWork()+"\t"
                         );

                 wait += count-test[i].getArrive();
                 cycle += count-test[i].getArrive()+test[i].getWork();

                 count += test[i].getWork();

                 if(count < test[i+1].getArrive())
                     count = test[i+1].getArrive();
                 temp += test[i].getName()+"   ";

             }

             System.out.println(test[length-1].getName()+"\t"
                     +test[length-1].getArrive()+"\t"
                     +test[length-1].getWork()+"\t"
                     +count+"\t"
                     +(count+test[length-1].getWork())+"\t"
                     +(count-test[length-1].getArrive())+"\t"
                     +(count-test[length-1].getArrive()+test[length-1].getWork())+"\t"
                     +(count-test[length-1].getArrive()+test[length-1].getWork())*1.0/test[length-1].getWork()+"\t"
                     );

             wait += count-test[length-1].getArrive();
             cycle += count-test[length-1].getArrive()+test[length-1].getWork();

             temp += test[test.length-1].getName()+"   ";

             System.out.println("\n所以最后先到先服务调度算法执行的顺序为:" + temp);
             System.out.println("平均等待时间为:" + wait*1.0/length + ",平均周转时间为:" + cycle*1.0/length);
         }

     }

     //SJF类非抢占式...............................................................
     public static class SJF{

         private int count;
         private String temp;

         public SJF(MyProcess test[]){
             Arrays.sort(test);

             count = test[0].getArrive();
             temp = "";

             int length = test.length;

             int wait = 0;    //总等待时间
             int cycle = 0;    //总周转时间

             System.out.print("------------------------------------------------------------------");
             System.out.println("------------------------------------------------------------------");
             System.out.println("最短作业优先调度算法非抢占式的结果为:");
             System.out.println("进程名字\t到达时间\t服务时间\t开始时间\t结束时间\t等待时间\t"
                     + "周转时间\t带权周转时间\t");

             for(int i=0; i<length-1; ++i){
                 System.out.println(test[i].getName()+"\t"
                         +test[i].getArrive()+"\t"
                         +test[i].getWork()+"\t"
                         +count+"\t"
                         +(count+test[i].getWork())+"\t"
                         +(count-test[i].getArrive())+"\t"
                         +(count-test[i].getArrive()+test[i].getWork())+"\t"
                         +(count-test[i].getArrive()+test[i].getWork())*1.0/test[i].getWork()+"\t"
                         );

                 wait += count-test[i].getArrive();
                 cycle += count-test[i].getArrive()+test[i].getWork();

                 count += test[i].getWork();

                 if(count < test[i+1].getArrive())
                     count = test[i+1].getArrive();
                 temp += test[i].getName()+"   ";

             }

             System.out.println(test[length-1].getName()+"\t"
                     +test[length-1].getArrive()+"\t"
                     +test[length-1].getWork()+"\t"
                     +count+"\t"
                     +(count+test[length-1].getWork())+"\t"
                     +(count-test[length-1].getArrive())+"\t"
                     +(count-test[length-1].getArrive()+test[length-1].getWork())+"\t"
                     +(count-test[length-1].getArrive()+test[length-1].getWork())*1.0/test[length-1].getWork()+"\t"
                     );

             wait += count-test[length-1].getArrive();
             cycle += count-test[length-1].getArrive()+test[length-1].getWork();

             temp += test[test.length-1].getName()+"   ";

             System.out.println("\n所以最后最短作业优先调度算法非抢占式执行的顺序为:" + temp);
             System.out.println("平均等待时间为:" + wait*1.0/length + ",平均周转时间为:" + cycle*1.0/length);
         }

     }

     //SJF类抢占式...............................................................
     public static class SJFS{

         private int count;
         private String temp;    //执行的顺序
         private int flag = 0;    //计数,用于判断是否所有进程执行完毕

         public SJFS(MyProcess test[]){

             int wait = 0;    //总等待时间
             int cycle = 0;    //总周转时间

             System.out.print("------------------------------------------------------------------");
             System.out.println("------------------------------------------------------------------");
             System.out.println("最短作业优先调度算法抢占式的结果为:");
             System.out.println("进程名字\t到达时间\t区间时长\t开始时间\t结束时间\t剩余服务时间\t等待时间\t"
                     + "周转时间\t带权周转时间\t");

             int length = test.length;    //长度

             whichComp="FCFS";
             Arrays.sort(test);    //按照进程的到达时间排序
             count = test[0].getArrive();    //count的值为程序最先到达的进程的到达时间

             whichComp="SJFS";
             Arrays.sort(test);     //所有进程按照剩余服务时间的长短排序

             String lastName = "";    //上一次执行的进程的名字
             for(int i=0; i<length; ++i){
                 if(test[i].getArrive()<=count){
                     lastName = test[i].getName();
                     System.out.print(test[i].getName() + "\t"
                             + test[i].getArrive() + "\t"
                             + test[i].getWork() + "\t"
                             + count + "\t");
                     temp = test[i].getName()+"   ";
                     break;
                 }
             }

             boolean done = false;

             while(true){
                 done = false;
                 ++count;

                 for(int j=0; j<length; ++j){
                     if(test[j].getName().equals(lastName)){
                         test[j].setRwork(test[j].getRwork()-1);    //剩余时间-1
                         if(test[j].getRwork() == 0){
                             ++flag;
                             test[j].setRwork(999999999);
                             System.out.println(count + "\t"
                                     + "0\t\t"
                                     + (count-test[j].getWork()-test[j].getArrive()) + "\t"    //等待时间
                                     + (count-test[j].getArrive()) + "\t"                        //周转时间
                                     + ((count-test[j].getArrive())*1.0/test[j].getWork()) + "\t" //带权周转
                                     );
                             wait += count-test[j].getWork()-test[j].getArrive();
                             cycle += count-test[j].getArrive();
                             done = true;
                         }
                         break;
                     }
                 }

                 if(flag==length){
                     break;
                 }

                 Arrays.sort(test);     //所有进程按照剩余服务时间的长短重新排序

                 for(int i=0; i<length; ++i){
                     if(test[i].getArrive()<=count){
                         if(lastName.equals(test[i].getName())){
 //                            System.out.println(test[i].getName());
                         }
                         else{
                             for(int j=0; j<length; ++j){
                                 if(test[j].getName().equals(lastName)){
                                     if(!done){
                                         System.out.println(count + "\t"
                                                 + test[j].getRwork()+ "\t\t"
                                                 + "未执行完\t" + "未执行完\t" + "未执行完\t");
                                     }
                                 }
                             }

                             lastName = test[i].getName();

                             System.out.print(test[i].getName() + "\t"
                                     + test[i].getArrive() + "\t"
                                     + test[i].getWork() + "\t"
                                     + count + "\t");
                             temp += test[i].getName()+"   ";
                         }
                         break;
                     }
                 }

             }

             System.out.println("\n所以最后最短作业优先调度算法抢占式执行的顺序为:" + temp);
             System.out.println("平均等待时间为:" + wait*1.0/length + ",平均周转时间为:" + cycle*1.0/length);

         }

     }

     //PSA类...............................................................
     public static class PSA{

         private int count;
         private String temp;

         public PSA(MyProcess test[]){
             Arrays.sort(test);

             count = test[0].getArrive();
             temp = "";

             int length = test.length;

             int wait = 0;    //总等待时间
             int cycle = 0;    //总周转时间

             System.out.print("------------------------------------------------------------------");
             System.out.println("------------------------------------------------------------------");
             System.out.println("优先级调度算法的结果为:");
             System.out.println("进程名字\t到达时间\t服务时间\t优先级\t开始时间\t结束时间\t等待时间\t"
                     + "周转时间\t带权周转时间\t");

             for(int i=0; i<length-1; ++i){
                 System.out.println(test[i].getName()+"\t"
                         +test[i].getArrive()+"\t"
                         +test[i].getWork()+"\t"
                         +test[i].getPriority()+"\t"
                         +count+"\t"
                         +(count+test[i].getWork())+"\t"
                         +(count-test[i].getArrive())+"\t"
                         +(count-test[i].getArrive()+test[i].getWork())+"\t"
                         +(count-test[i].getArrive()+test[i].getWork())*1.0/test[i].getWork()+"\t"
                         );

                 wait += count-test[i].getArrive();
                 cycle += count-test[i].getArrive()+test[i].getWork();

                 count += test[i].getWork();

                 if(count < test[i+1].getArrive())
                     count = test[i+1].getArrive();
                 temp += test[i].getName()+"   ";

             }

             System.out.println(test[length-1].getName()+"\t"
                     +test[length-1].getArrive()+"\t"
                     +test[length-1].getWork()+"\t"
                     +test[length-1].getPriority()+"\t"
                     +count+"\t"
                     +(count+test[length-1].getWork())+"\t"
                     +(count-test[length-1].getArrive())+"\t"
                     +(count-test[length-1].getArrive()+test[length-1].getWork())+"\t"
                     +(count-test[length-1].getArrive()+test[length-1].getWork())*1.0/test[length-1].getWork()+"\t"
                     );

             wait += count-test[length-1].getArrive();
             cycle += count-test[length-1].getArrive()+test[length-1].getWork();

             temp += test[test.length-1].getName()+"   ";

             System.out.println("\n所以最后优先级调度算法执行的顺序为:" + temp);
             System.out.println("平均等待时间为:" + wait*1.0/length + ",平均周转时间为:" + cycle*1.0/length);
         }

     }

     //HRR类...............................................................
     public static class HRR{

         private int count;
         private String temp;

         public HRR(MyProcess test[], MyProcess tests[]){

             DecimalFormat df = new DecimalFormat("#.###");     //小数保留三位小数  

             Arrays.sort(tests);

             count = tests[0].getBegin();
             temp = "";

             int length = test.length;

             int wait = 0;    //总等待时间
             int cycle = 0;    //总周转时间

             System.out.print("------------------------------------------------------------------");
             System.out.println("------------------------------------------------------------------");
             System.out.println("最高响应比调度算法的结果为:");

             for(int tur = 1; tur<=length; ++tur){
                 System.out.println("\n正在执行第"+tur+"个进程:"+tests[0].getName());
                 System.out.println("进程名字\t到达时间\t服务时间\t响应比\t开始时间\t结束时间\t等待时间\t"
                         + "周转时间\t带权周转时间\t");    

                 for(int i=0; i<length; ++i) {

                     if(test[i].getFlag()){    //已执行
                         System.out.println(test[i].getName()+"\t"
                                 +test[i].getArrive()+"\t"
                                 +test[i].getWork()+"\t"
                                 +"已执行完\t"
                                 +test[i].getBegin()+"\t"
                                 +(test[i].getBegin()+test[i].getWork())+"\t"
                                 +(test[i].getBegin()-test[i].getArrive())+"\t"
                                 +(test[i].getBegin()-test[i].getArrive()+test[i].getWork())+"\t"
                                 +df.format((test[i].getBegin()-test[i].getArrive()+test[i].getWork())*1.0/test[i].getWork())+"\t"
                                 );
                         continue;
                     }

                     else if(test[i].getArrive()>count){    //未到达
                         System.out.println(test[i].getName()+"\t"
                                 +test[i].getArrive()+"\t"
                                 +test[i].getWork()+"\t"
                                 +"未到达\t"
                                 +"未到达\t"
                                 +"未到达\t"
                                 +"未到达\t"
                                 +"未到达\t"
                                 +"未到达\t"
                                 );
                         continue;
                     }

                     else{
                         //执行该进程
                         if(test[i].getName().equals(tests[0].getName())){
                             System.out.println(test[i].getName()+"\t"
                                     +test[i].getArrive()+"\t"
                                     +test[i].getWork()+"\t"
                                     +df.format(test[i].getHrr())+"\t"
                                     +test[i].getBegin()+"\t"
                                     +(test[i].getBegin()+test[i].getWork())+"\t"
                                     +(test[i].getBegin()-test[i].getArrive())+"\t"
                                     +(test[i].getBegin()-test[i].getArrive()+test[i].getWork())+"\t"
                                     +df.format((test[i].getBegin()-test[i].getArrive()+test[i].getWork())*1.0/test[i].getWork())+"\t"
                                     );

                             wait += test[i].getBegin()-test[i].getArrive();
                             cycle += test[i].getBegin()-test[i].getArrive()+test[i].getWork();
                             test[i].setFlag();
                             tests[0].setFlag();
                         }

                         else{
                             System.out.println(test[i].getName()+"\t"
                                     +test[i].getArrive()+"\t"
                                     +test[i].getWork()+"\t"
                                     +df.format(test[i].getHrr())+"\t"
                                     +"未开始\t"
                                     +"未开始\t"
                                     +"未开始\t"
                                     +"未开始\t"
                                     +"未开始\t"
                                     );
                         }

                     }
                 }

                 int count1=count+tests[0].getWork();

                 for(int j=tests.length-1; j>=1; --j){
                     if(tests[j].getFlag()){
                         ;
                     }
                     else{
                         if(count1 >= tests[j].getArrive()){    //进程已到
                             tests[j].setBegin(count1);
                             int x;
                             for(x=0; x<length; ++x){
                                 if(tests[j].getName().equals(test[x].getName()))
                                     break;
                             }
                             test[x].setBegin(count1);
                             tests[j].setHrr();
                             test[x].setHrr();
                             count = count1;
                         }
                         else{
                             count = tests[j].getBegin();
                         }
                     }
                 }

                 temp += tests[0].getName()+"   ";

                 //改变执行过的进程数据,使再次排序时排在未执行数据的后面
                 tests[0].setArrive(999999999);
                 tests[0].setBegin(999999999);
                 tests[0].setHrr(-1.0);

                 Arrays.sort(tests);    //重新排序

             }

             System.out.println("\n所以最后最高响应比调度算法执行的顺序为:" + temp);
             System.out.println("平均等待时间为:" + wait*1.0/length + ",平均周转时间为:" + cycle*1.0/length);
         }

     }

     //RR类...............................................................
     public static class RR{

         private int count;
         private String temp;

         public RR(MyProcess test[]){

             count = 0;
             temp = "";

             int length = test.length;

             int wait = 0;    //总等待时间
             int cycle = 0;    //总周转时间

             int tur = 1;    //第几趟

             System.out.print("------------------------------------------------------------------");
             System.out.println("------------------------------------------------------------------");
             System.out.println("轮转法调度算法的结果为:");
             System.out.println("第"+tur+"趟结果\n进程名字\t到达时间\t服务时间\t开始时间\t结束时间\t剩余所需服务时间\t"
                     + "等待时间\t周转时间\t带权周转时间\t");

             int tempCount=0;    //记录是否还有进程未执行完

             int i=0;

             while(true){

                 if(test[i].getRwork()>timeSlice){

                     test[i].setRwork(test[i].getRwork()-timeSlice);

                     System.out.println(test[i].getName()+"\t"
                             +test[i].getArrive()+"\t"
                             +test[i].getWork()+"\t"
                             +count+"\t"
                             +(count+timeSlice)+"\t"
                             +test[i].getRwork()+"\t\t"
                             +"未执行完\t"
                             +"未执行完\t"
                             +"未执行完\t"
                             );

                     count += timeSlice;
                     temp += test[i].getName()+"  ";

                     ++i;
                     if(i==length){
                         i=0;
                         ++tur;
                         System.out.println("第"+tur+"趟结果\n进程名字\t到达时间\t服务时间\t开始时间\t结束时间\t"
                                 + "剩余所需服务时间\t等待时间\t周转时间\t带权周转时间\t");
                     }
                 }

                 else if(test[i].getRwork()>0){

                     System.out.println(test[i].getName()+"\t"
                             +test[i].getArrive()+"\t"
                             +test[i].getWork()+"\t"
                             +count+"\t"
                             +(count+test[i].getRwork())+"\t"
                             +"0\t\t"
                             +(count+test[i].getRwork()-test[i].getWork()-test[i].getArrive())+"\t"
                             +(count+test[i].getRwork()-test[i].getArrive())+"\t"
                             +(count+test[i].getRwork()-test[i].getArrive())*1.0/test[i].getWork()+"\t"
                             );

                     wait += count+test[i].getRwork()-test[i].getWork()-test[i].getArrive();
                     cycle += count+test[i].getRwork()-test[i].getArrive();

                     count += test[i].getRwork();
                     temp += test[i].getName()+"  ";

                     test[i].setRwork(0);

                     ++i;
                     if(i==length){
                         i=0;
                         ++tur;
                         System.out.println("第"+tur+"趟结果\n进程名字\t到达时间\t服务时间\t开始时间\t结束时间\t"
                                 + "剩余所需服务时间\t等待时间\t周转时间\t带权周转时间\t");
                     }

                     ++tempCount;
                     if(tempCount == length)
                         break;
                 }
                 else{
                     ++i;
                     if(i==length){
                         i=0;
                         ++tur;
                         System.out.println("第"+tur+"趟结果\n进程名字\t到达时间\t服务时间\t开始时间\t结束时间\t"
                                 + "剩余所需服务时间\t等待时间\t周转时间\t带权周转时间\t");
                     }
                     continue;
                 }
             }

             System.out.println("\n所以最后轮转法调度算法执行的顺序为:" + temp);
             System.out.println("平均等待时间为:" + wait*1.0/length + ",平均周转时间为:" + cycle*1.0/length);
         }

     }    

     //主函数................................................................
     public static void main(String[] args) {
         Scanner input = new Scanner(System.in);

         while(true){
             int n;    //进程数
             while(true){
                 try{
                     System.out.print("输入进程个数:");
                     n = input.nextInt();
                     break;
                 } catch(Exception e){
                     System.out.println("输入不合法,请重新输入!");
                     input.nextLine();
                 }
             }
             MyProcess test[] = new MyProcess[n];    //进程数组
             MyProcess tests[] = new MyProcess[n];    //临时进程数组(HRR用)

             System.out.println("您想执行何种操作:");
             System.out.println("1.FCFS(先到先服务调度算法)");
             System.out.println("2.SJF(最短作业优先调度算法,非抢占式)");
             System.out.println("3.PSA(优先级调度算法)");
             System.out.println("4.RR(轮转法调度算法)");
             System.out.println("5.HRR(最高响应比调度算法)");
             System.out.println("6.SJF(最短作业优先调度算法,抢占式)");
             System.out.println("7.退出");
             int which;

             try{
                 which = input.nextInt();
             } catch(Exception e){
                 System.out.println("输入不合法,请重新输入!");
                 input.nextLine();
                 continue;
             }

             //FCFS
             if(which == 1){
                 for(int i=0; i<n; ++i){
                     test[i] = new MyProcess("P"+(i+1));
                     System.out.print("请输入P" + (i+1) + "的到达时间和服务时间:");
                     test[i].setArrive(input.nextInt());
                     test[i].setWork(input.nextInt());
                 }
                 whichComp = "FCFS";
                 new FCFS(test);
             }
             //SJF,非抢占式
             else if(which == 2){
                 for(int i=0; i<n; ++i){
                     test[i] = new MyProcess("P"+(i+1));
                     System.out.print("请输入P" + (i+1) + "的到达时间和服务时间:");
                     test[i].setArrive(input.nextInt());
                     test[i].setWork(input.nextInt());
                 }
                 whichComp = "SJF";
                 new SJF(test);
             }
             //PSA
             else if(which == 3){
                 for(int i=0; i<n; ++i){
                     test[i] = new MyProcess("P"+(i+1));
                     System.out.print("请输入P" + (i+1) + "的到达时间和服务时间和优先级:");
                     test[i].setArrive(input.nextInt());
                     test[i].setWork(input.nextInt());
                     test[i].setPriority(input.nextInt());
                 }
                 whichComp = "PSA";
                 new PSA(test);
             }
             //RR
             else if(which == 4){
                 System.out.print("请输入时间片的大小:");
                 timeSlice = input.nextInt();

                 for(int i=0; i<n; ++i){
                     test[i] = new MyProcess("P"+(i+1));
                     System.out.print("请输入P" + (i+1) + "的服务时间(剩余时间):");
                     test[i].setWork(input.nextInt());
                     test[i].setRwork(test[i].getWork());
                 }
                 whichComp = "RR";
                 new RR(test);
             }
             //HRR
             else if(which == 5){
                 for(int i=0; i<n; ++i){
                     test[i] = new MyProcess("P"+(i+1));
                     tests[i] = new MyProcess("P"+(i+1));
                     System.out.print("请输入P" + (i+1) + "的到达时间和服务时间:");
                     int a = input.nextInt();
                     test[i].setArrive(a);
                     tests[i].setArrive(a);
                     int b = input.nextInt();
                     test[i].setWork(b);
                     tests[i].setWork(b);
                     //设置开始时间和响应比
                     test[i].setBegin(a);
                     test[i].setHrr();
                     tests[i].setBegin(a);
                     tests[i].setHrr();
                 }
                 whichComp = "HRR";
                 new HRR(test,tests);
             }
             //SJF,抢占式
             else if(which == 6){
                 for(int i=0; i<n; ++i){
                     test[i] = new MyProcess("P"+(i+1));
                     System.out.print("请输入P" + (i+1) + "的到达时间和服务时间:");
                     test[i].setArrive(input.nextInt());
                     test[i].setWork(input.nextInt());
                     test[i].setRwork(test[i].getWork());    //设置剩余时间
                 }
                 whichComp = "SJFS";
                 new SJFS(test);
             }
             else if(which == 7){
                 System.out.println("程序结束!");
                 break;
             }
             else {
                 System.out.println("输入不合法,请重新输入!");
             }

             System.out.print("------------------------------------------------------------------");
             System.out.println("------------------------------------------------------------------");
             System.out.print("------------------------------------------------------------------");
             System.out.println("------------------------------------------------------------------\n\n");
         }

         input.close();

     }

 }

部分运行结果:

相同数据的最高响应比结果为

操作系统中的几种调度算法(JAVA版)的更多相关文章

  1. 南阳ACM 题目8:一种排序 Java版

    一种排序 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都是整数:现 ...

  2. 剑指offer第二版面试题1:数组中重复的数字(JAVA版)

    题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复的次数.请找出数组中任意一个重复的数字.例如如果输入长度为7的数组{ ...

  3. 剑指offer:1.找出数组中重复的数(java版)

    数组中重复的数:题目:找出数组中重复的数,题目描述:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任 ...

  4. 单例模式的几种实现-Java版

    目录 关键点 饿汉式 懒汉式 双检锁 静态内部类单例模式 枚举方式 关键点 私有化构造器 通过静态方法或枚举返回单例类对象 确保单例类对象只有一个,尤其在多线程环境下. 确保每个类被序列化不会重新创建 ...

  5. 剑指offer第二版面试题2:数组中重复的数字(JAVA版)

    题目:在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的.请找出数组中任意一个重复的数字,但是不能修改输入的数组.例如,如果输入长度为8的数组{2,3,5,4,3 ...

  6. Java实现操作系统中四种动态内存分配算法:BF+NF+WF+FF

    1 概述 本文是利用Java实现操作系统中的四种动态内存分配方式 ,分别是: BF NF WF FF 分两部分,第一部分是介绍四种分配方式的概念以及例子,第二部分是代码实现以及讲解. 2 四种分配方式 ...

  7. c#中的23种设计模式

    C# 23种设计模式汇总 创建型模式 工厂方法(Factory Method) 在工厂方法模式中,工厂方法用来创建客户所需要的产品,同时还向客户隐藏了哪种具体产品类将被实例化这一细节.工厂方法模式的核 ...

  8. Oracle Berkeley DB Java 版

    Oracle Berkeley DB Java 版是一个开源的.可嵌入的事务存储引擎,是完全用 Java 编写的.它充分利用 Java 环境来简化开发和部署.Oracle Berkeley DB Ja ...

  9. 剑指Offer面试题15(Java版):链表中倒数第K个结点

    题目: 输入一个链表.输出该链表中倒数第k哥结点.  为了符合大多数人的习惯,本题从1開始计数.即链表的尾结点是倒数第1个结点. 比如一个链表有6个结点.从头结点開始它们的值依次是1.2.3,4,5, ...

随机推荐

  1. H5新特性之webWorker

    众所周知javascript是单线程语言,这就是js开发难度较低的原因了,因为不需要解决多线程的资源共享问题(例如死锁),但是单线程性能并不好,因此多了一个webWorker实现js的多进程来提升js ...

  2. MHA 安装与简单使用

    MHA 在过去几年一直用的比较火,特别是在在传统复制的那个年代.至从有了GTID好像我们也可以把MHA给忘记了,但是很多企业现在还是在用的比较多.每个公司的MHA玩法也不太一样,但是本质都是差不多了. ...

  3. Angular 学习笔记 ( PWA + App Shell )

    PWA (Progressive Web Apps) 是未来网页设计的方向. 渐进式网站. Angular v5 开始支持 pwa 网站 (所谓支持意思是说有一些 build in 的方法和规范去实现 ...

  4. spring-oauth-server实践:access_token的有效期分析

    1.access_token有效期检查 用expiration和new Date()比较!!!!!! 分析目标-->expiration什么时候设置,设置规则如何配置!!!!!!! 2.acce ...

  5. 从源码看JDK提供的线程池(ThreadPoolExecutor)

    一丶什么是线程池 (1)博主在听到线程池三个字的时候第一个想法就是数据库连接池,回忆一下,我们在学JavaWeb的时候怎么理解数据库连接池的,数据库创建连接和关闭连接是一个比较耗费资源的事情,对于那些 ...

  6. ES6 new syntax of let and const (one)

    variable declarations : let, const,and block scope why we redefine the way about declarations? funct ...

  7. [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  8. 解决:GitHub 远程端添加了 README.md 文件后,本地 push 代码时出现错误

    一.错误描述 To github.com:compassblog/PythonExercise.git ! [rejected] master -> master (fetch first) e ...

  9. github的简单使用

    查了好多入门教程(图文并茂可以了解一些基本步骤),感觉逻辑欠缺,(很多东西跟着教程了解会用了,不了解逻辑,只是会了这一个,其他的还是很蒙),来一起理一理把 1.第一步下载并注册(这个自己解决) 2.用 ...

  10. 初学servlet之@WebServlet传参

    package app01a;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletCon ...