//先来先服务算法 #include <stdio.h> #include <stdlib.h> #define N 24 main() { int i,n; int reachtime[N],needtime[N],starttime[N],finishtime[N],cycletime[N];//周转时间 float cltime[N];//带权周转时间 char name[N]; printf("\n 请输入作业的个数:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n name:"); scanf("%s",&name[i]); printf("\n reachtime:"); scanf("%d",&reachtime[i]); printf("\n needtime:"); scanf("%d",&needtime[i]); if(i==0) { starttime[i]=reachtime[i]; } finishtime[i]=starttime[i]+needtime[i]; cycletime[i]=finishtime[i]-reachtime[i]; cltime[i]=cycletime[i]*1.0/needtime[i]; starttime[i+1]=finishtime[i]; } printf("\n\tname reachtime needtime startime finishtime cycletime cltime "); for(i=0;i<n;i++) { printf("\n\t %c\t %d\t %d\t %d\t %d\t %d\t %.2f",name[i],reachtime[i],needtime[i], starttime[i],finishtime[i],cycletime[i],cltime[i]); } }
//最短作业优先算法 #include <stdio.h> #include <stdlib.h> #define N 24 main() { int i,j,n,temp; int reachtime[N],needtime[N],starttime[N],finishtime[N],cycletime[N]; float cltime[N]; char name[N],ctemp; printf("\n 请输入作业的个数:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n name:"); scanf("%s",&name[i]); printf("\n reachtime:"); scanf("%d",&reachtime[i]); printf("\n needtime:"); scanf("%d",&needtime[i]); } for(i=0;i<n;i++) { for(j=1;j<n-1-i;j++) { if(needtime[j] > needtime[j+1]) { temp = needtime[j+1]; needtime[j+1] = needtime[j]; needtime[j] = temp; ctemp=name[j+1]; name[j+1]=name[j]; name[j]=ctemp; temp=reachtime[j+1]; reachtime[j+1]=reachtime[j]; reachtime[j]=temp; } else if(needtime[j]==needtime[j+1]&&reachtime[j]>reachtime[j+1]) { temp = needtime[j+1]; needtime[j+1] = needtime[j]; needtime[j] = temp; ctemp=name[j+1]; name[j+1]=name[j]; name[j]=ctemp; temp=reachtime[j+1]; reachtime[j+1]=reachtime[j]; reachtime[j]=temp; } } } for(i=0;i<n;i++) { if(i==0) { starttime[i]=reachtime[i]; } finishtime[i]=starttime[i]+needtime[i]; cycletime[i]=finishtime[i]-reachtime[i]; cltime[i]=cycletime[i]*1.0/needtime[i]; starttime[i+1]=finishtime[i]; } printf("\n\tname reachtime needtime startime finishtime cycletime cltime "); for(i=0;i<n;i++) { printf("\n\t %c\t %d\t %d\t %d\t %d\t %d\t %.2f",name[i],reachtime[i],needtime[i], starttime[i],finishtime[i],cycletime[i],cltime[i]); } }
时间: 2024-11-05 06:06:15