#include<stdio.h>
#include<stdlib.h>
#define N 100
typedef struct process
{
int num;
int time;
int start;
int end;
int begin;
int freetime;
int arrivetime;
int finish;
int turn;
double reaction;
double average;
}P;
P a[N],b;
int wordtime=0;
void hrrn(int i,int j,int t,int n){
a[0].finish=a[0].arrivetime+a[0].freetime;//给予a[0]初值
wordtime=a[0].finish;
a[0].turn=a[0].time;
a[0].average=(float)a[0].turn/a[0].time;
for(i=1;i<n;i++)//执行
{
for(j=i+1;j<n;j++)
{
if(a[j].arrivetime<=a[i-1].finish)
{
for(t=i;t<=j;t++)
{
a[t].reaction=(float)(a[t-1].finish-a[t].arrivetime+a[t].time)/a[t].time;
if(a[t].reaction<a[j].reaction)
{
b=a[j];
a[j]=a[t];
a[t]=b;
}
}
}
}
if(a[i].arrivetime<a[i-1].finish)
{
a[i].finish=a[i-1].finish+a[i].freetime;
a[i].turn=a[i].finish-a[i].arrivetime;
}
else
{
a[i].finish=a[i].arrivetime+a[i].freetime;
a[i].turn=a[i].time;
}
a[i].average=(float)a[i].turn/a[i].time;
wordtime=a[i].finish;
}
}
void main()
{
int n=5,i,t,sum=0,num=2,j,x;
double average=0,turn=0;
printf("请输入进程个数");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\n");
printf("请输入第%d个进程到达时间:",i+1);
scanf("%d",&a[i].arrivetime);
printf("请输入第%d个进程需要时间:",i+1);
scanf("%d",&a[i].time);
printf("\n");
a[i].start=a[i].arrivetime;
a[i].freetime=a[i].time;//剩余时间赋值
a[i].num=i+1;
a[i].start=a[i].end=0;
}//输入数据
for(i=0;i<n;i++)
{
for(t=i+1;t<n;t++)
{
if(a[t].arrivetime<a[i].arrivetime)
{
b=a[i];
a[i]=a[t];
a[t]=b;
}
else if(a[t].arrivetime==a[i].arrivetime)
{
if(a[i].time>a[t].time)
{
b=a[i];
a[i]=a[t];
a[t]=b;
}
}
}
}//排序
hrrn(i,j,t,n);
printf("进程号\t完成时间\t周转时间\t带权周转时间\n");
for(i=0;i<n;i++)
printf("%d\t%d\t\t%d\t\t%.2f\n",a[i].num,a[i].finish,a[i].turn,a[i].average);
for(i=0;i<n;i++)
{
turn=turn+(float)a[i].turn;
average=average+(float)a[i].turn/a[i].time;
}
turn=turn/n;
average=average/n;
printf("平均周转时间\t平均带权周转时间\n");
printf("%.2f\t\t%.2f\n",turn,average);
}