Single CPU, multi-tasking
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 582 Accepted Submission(s): 204
At 0th millisecond, task 1 gets the CPU. After running for 1 millisecond, it still needs 0.5 milliseconds to complete.
At 1st millisecond, task 2 gets the CPU. After running for 1 millisecond, it still needs 3.2 milliseconds to complete.
At 2st millisecond, task 3 gets the CPU. After running for 1 millisecond, it still needs 1.8 milliseconds to complete.
At 3rd millisecond, task 1 comes back to CPU again. After 0.5 millisecond of running, it is finished and will never need the CPU.
At 3.5 millisecond, task 2 gets the CPU again. After running for 1 millisecond, it still needs 2.2 milliseconds to complete.
At 4.5 millisecond, it’s time for task 3 to run. After 1 millisecond, it still needs 0.8 milliseconds to complete.
At 5.5 millisecond, it’s time for task 2 to run. After 1 millisecond, it still needs 1.2 milliseconds to complete.
At 6.5 millisecond, time for task 3. It needs 0.8 millisecond to complete, so task 3 is finished at 7.3 milliseconds.
At 7.3 millisecond, task 2 takes the CPU and keeps running until it is finished.
At 8.5 millisecond, all tasks are finished.
Tuntun decided to make a simple iPhone multi-tasking OS himself, but at first, he needs to know the finishing time of every task. Can you help him?
The following 2×T lines represent T test cases. The first line of each test case is a integer N (0<N <= 100) which represents the number of tasks, and the second line contains N real numbers indicating the time needed for each task. The time is in milliseconds, greater than 0 and less than 10000000.
<span style="font-family:Arial;">#include<stdio.h>
#include<math.h>
#define MAX 200
int main(){
int t;
scanf("%d",&t);
int i;
for(i=0;i<t;i++){
int n;
double reserve[MAX];
double marks=0;
double cost[MAX];
scanf("%d",&n);
int j;
for(j=0;j<n;j++){
scanf("%lf",&reserve[j]);
}
int k;
/*計算某個任務總共完成需要的時間,每個任務所在的序列的行相加就是此任務輸入的時間
此後做其他序列上相對此任務的位置的判定,不同的位置所花費的時間是不同的,總共有n個
任務,則需要分別計算這n個任務*/
for(j=0;j<n;j++){
cost[j]=reserve[j];
for(k=0;k<n;k++){
if(k!=j){
/*當某個位置時間花銷少於目標位置的情況*/
if(floor(reserve[k]+0.999)<floor(reserve[j]+0.999))//floor(double)函數取得是某個數的下限,即不大於某個數
cost[j]=cost[j]+reserve[k];
/*當某個位置的時間花銷和目標位置在一輪的時間片上的情況*/
else if(floor(reserve[k]+0.999)==floor(reserve[j]+0.999)){
if(k<j)
cost[j]=cost[j]+reserve[k];
else
cost[j]=cost[j]+(floor(reserve[k]+0.999)-1);
}
/*當某個位置的時間花銷多於目標位置的情況*/
else if(floor(reserve[k]+0.999)>floor(reserve[j]+0.999)){
if(k>j)
cost[j]=cost[j]+(floor(reserve[j]+0.999)-1);
else
cost[j]=cost[j]+floor(reserve[j]+0.999);
}
}
}
}
printf("Case %d:\n",i+1);
for(j=0;j<n;j++){
printf("%.2lf\n",cost[j]);
}
}
return 0;
}</span>