题目描述
已知n个进程的名称、到达时间和服务时间,通过先来先服务算法对这n个进程进行调度并输出调度顺序
输入
进程的个数n
接下来的n行,每行有一个字符串和两个正整数
输出
输出n+1行,
第一行输出 进程名称 到达时间 服务时间 开始时间 完成时间 周转时间 带权周转时间
接下来n行在输入的数据基础上添加开始时间、完成时间、周转时间以及带权周转时间
样例输入
请输入进程数量:2
进程名称 到达时间 服务时间
a 0 4
b 1 3
c 2 5
d 3 2
样例输出
进程名称 到达时间 服务时间 开始时间 完成时间 周转时间 带权周转时间
a 0 4 0 4 4 1
b 1 3 4 7 6 2
c 2 5 7 12 10 2
d 3 2 12 14 11 5.5
代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class FCFS {
//进程数量
private int mentationnumber;
//进程
private ArrayList mentationList;
public FCFS(){
//初始化进程信息和排序
init();
//计算进程的完成时间、周转时间、带权周转时间
calc();
//输出
removal();
}
private void init() {
Scanner sc = new Scanner(System.in);
System.out.print(“请输入进程数量:”);
mentationnumber = sc.nextInt();
System.out.println(“进程名称 到达时间 服务时间”);
mentationList = new ArrayList();
for(int i = 0; i < mentationnumber; i++){
String name = sc.next();
int arrive = sc.nextInt();
int service = sc.nextInt();
Mentation mentation = new Mentation(name,arrive,service);
mentationList.add(mentation);
}
Collections.sort(mentationList);
}
private void calc() {
for(int i = 0; i < mentationList.size(); i++){
int arrival = mentationList.get(i).getArrivaltime();
int service = mentationList.get(i).getServicetime();
if(i == 0){
mentationList.get(i).setStarttime(arrival);
mentationList.get(i).setEndtime(arrival + service);
mentationList.get(i).setTurnaroundtime(mentationList.get(i).getEndtime() - arrival);
double weight = (double)mentationList.get(i).getTurnaroundtime() / service;
mentationList.get(i).setWeightturnaroundtime(weight);
}
else{
int end = mentationList.get(i-1).getEndtime();
mentationList.get(i).setStarttime(end);
mentationList.get(i).setEndtime(end + service);
mentationList.get(i).setTurnaroundtime(mentationList.get(i).getEndtime() - arrival);
double weight = (double)mentationList.get(i).getTurnaroundtime() / service;
mentationList.get(i).setWeightturnaroundtime(weight);
}
}
}
private void removal() {
System.out.println(“进程名称 到达时间 服务时间 开始时间 完成时间 周转时间 带权周转时间”);
for(Mentation mentation:mentationList){
System.out.println(mentation.getProcessname()+" “+mentation.getArrivaltime()+” “+mentation.getServicetime()+” "
+mentation.getStarttime()+" “+mentation.getEndtime()+” “+mentation.getTurnaroundtime()+” "+mentation.getWeightturnaroundtime());
}
}
public static void main(String[] args) {
new FCFS();
}
}
public class Mentation implements Comparable{
//进程名
private String Processname;
//到达时间
private int arrivaltime;
//服务时间
private int servicetime;
//开始时间
private int starttime;
//结束时间
private int endtime;
//周转时间
private int turnaroundtime;
//带权周转时间
private double weightturnaroundtime;
public String getProcessname() {
return Processname;
}
public void setProcessname(String processname) {
Processname = processname;
}
public int getArrivaltime() {
return arrivaltime;
}
public void setArrivaltime(int arrivaltime) {
this.arrivaltime = arrivaltime;
}
public int getServicetime() {
return servicetime;
}
public void setServicetime(int servicetime) {
this.servicetime = servicetime;
}
public int getStarttime() {
return starttime;
}
public void setStarttime(int starttime) {
this.starttime = starttime;
}
public int getEndtime() {
return endtime;
}
public void setEndtime(int endtime) {
this.endtime = endtime;
}
public int getTurnaroundtime() {
return turnaroundtime;
}
public void setTurnaroundtime(int turnaroundtime) {
this.turnaroundtime = turnaroundtime;
}
public double getWeightturnaroundtime() {
return weightturnaroundtime;
}
public void setWeightturnaroundtime(double weightturnaroundtime) {
this.weightturnaroundtime = weightturnaroundtime;
}
public Mentation(String processname, int arrivaltime, int servicetime) {
this.Processname = processname;
this.arrivaltime = arrivaltime;
this.servicetime = servicetime;
}
/**
* 按到达时间进行排序
* @param that
* @return
*/
@Override
public int compareTo(Mentation that) {
if(this.arrivaltime - that.arrivaltime > 0){
return 1;
}
else{
return -1;
}
}
}