2: Job scheduling algorithm – Short work priority
input N(N>0) Jobs , Enter the name of each job , arrival time , service time , According to the short job first algorithm , Calculate the completion time of each job , Turnaround time , Weighted turnaround time ( retain 2 Decimal places ).
Input format :
Enter the number of jobs in the first line , Enter the name of the job in the second line , Enter the arrival time on the third line , On the fourth line, enter the service time .
Output format :
Sort by arrival time from small to large , The first line outputs the name of the job , Second line output arrival time , The third line outputs the service time , Fourth line output completion time , Line 5 output completion time , Line 6 output weighted turnover time .
sample input :
Here is a set of inputs . for example :
5 A B C D E 0 1 2 3 4 4 3 1 2 4
sample output :
The corresponding output is given here . for example :
do trade name :A B C D E arrival time :0 1 2 3 4 service time :4 3 1 2 4 Completion time :4 10 5 7 14 Turnaround time :4 9 3 4 10
Weighted turnaround time :1.00 3.00 3.00 2.00 2.50
#include <bits/stdc++.h> using namespace std; struct process{ char name[16];
// name double come_time; // arrival time double run_time;// Running time double finish_time;// Completion time
double circle_time;// Turnaround time double weight_circletime;// Weighted turnaround time int finished;
// Whether the program is executed (0) Is not implemented , Otherwise, it has been implemented }Process[1024]; int n; // Input in n Processes // input void Input(){
cin>>n; for(int i=0;i<n;i++){ cin>>Process[i].name; Process[i].finished = 0;
// The programs entered are not executed } for(int i=0;i<n;i++){ cin>>Process[i].come_time; } for(int i=
0;i<n;i++){ cin>>Process[i].run_time; } } // output void Output(){ printf(" do trade name :");
for(int i=0;i<n;i++){ cout<<Process[i].name; if(i<n-1){ printf(" "); } } printf(
"\n"); printf(" arrival time :"); for(int i=0;i<n;i++){ cout<<Process[i].come_time; if(i<n
-1){ printf(" "); } } printf("\n"); printf(" service time :"); for(int i=0;i<n;i++){ cout
<<Process[i].run_time; if(i<n-1){ printf(" "); } } printf("\n"); printf(" Completion time :")
; for(int i=0;i<n;i++){ cout<<Process[i].finish_time; if(i<n-1){ printf(" "); }
} printf("\n"); printf(" Turnaround time :"); for(int i=0;i<n;i++){ cout<<Process[i].
circle_time; if(i<n-1){ printf(" "); } } printf("\n"); printf(" Weighted turnaround time :"); for(
int i=0;i<n;i++){ printf("%.2f",Process[i].weight_circletime); if(i < n-1) {
printf(" "); } } } bool cmp(process p1,process p2){ // Sort the arriving processes according to the arrival time return p1
.come_time<p2.come_time; } int main(){ Input(); sort(Process,Process+n,cmp); int
finished_count= 0; // Record the number of completed processes int unfinish_pos = 0; // Record the location of incomplete processes double
now_time; while(finished_count<n){ if(now_time<Process[unfinish_pos].come_time){
// Update the arrival time of new programs that arrive before the completion of the previous program to now_time now_time = Process[unfinish_pos].come_time;
} double min_run_time = INT_MAX; // Compare the shortest running time as an intermediate quantity ( Give a big value at the beginning ) int pos = 0;
// Used to record the location of the next process to run for(int i = unfinish_pos;(i < n && now_time >= Process[i].
come_time);i++){// // Because the time is updated , Therefore, the comparison is also applicable to finding the job with the shortest running time among the earliest batch of jobs if(Process[i].
finished== 1) continue ; if(Process[i].run_time < min_run_time){ min_run_time =
Process[i].run_time; pos = i; } } { //
// Because the update current time must be greater than or equal to the earliest arrival time , So directly add the running time of the program that arrives later now_time += Process[pos].run_time;
Process[pos].finish_time = now_time; Process[pos].circle_time = now_time -
Process[pos].come_time; Process[pos].weight_circletime = Process[pos].
circle_time/ Process[pos].run_time; Process[pos].finished = 1; if(pos ==
unfinish_pos){ // If pos Is located exactly at unfinish_pos place , Then unfinish_pos Record to next process unfinish_pos
= pos + 1; } finished_count++; } } Output(); return 0; }

Technology