编程介的小学生 2017-10-02 04:56 采纳率: 20.5%
浏览 759
已采纳

Single CPU, multi-tasking

Description

Tuntun is a big fan of Apple Inc. who owns almost all kinds of products the company published. Fascinated by the amazing user experience and fabulous user interface, he spent every nickel of his pocket money on his iMac and iPhone. A few days ago, Apple released their latest iPhone OS 4.0. Tuntun noticed that the most significant new feature of iPhone OS 4.0 is multi-tasking support. He was curious about why the same device with a single core CPU can support multi-tasking under the new operating system. With his clever head, he found out a simple solution. The OS doesn’t have to let the CPU run several tasks exactly at the same time. What the OS should do is just to let the user feel that several tasks are running at the same time. In order to do that, the OS assigns the CPU to the tasks in turn. When the acts of reassigning a CPU from one task to another occur frequently enough, the illusion of parallelism is achieved. Let’s suppose that the OS makes each task run on the CPU for one millisecond every time in turn, and when a task is finished, the OS assigns the CPU to another task immediately. Now if there are 3 tasks which respectively need 1.5, 4.2 and 2.8 millisecond to complete, then the whole process is as follows:

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?
Input

The first line contains only one integer T indicates the number of test cases.
The following 2×T lines represent T test cases. The first line of each test case is a integer N (0 < N *lt;= 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.
Output

For each test case, first output “Case N:”, N is the case No. starting form 1. Then print N lines each representing a task’s finishing time, in the order correspondent to the order of tasks in the input. The results should be rounded to 2 digits after decimal point, and you must keep 2 digits after the decimal point.
Sample Input

2
3
1.5 4.2 2.8
5
3.5 4.2 1.6 3.8 4.4
Sample Output

Case 1:
3.50
8.50
7.30
Case 2:
14.10
17.10
7.60
15.90
17.50

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-10-17 17:10
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看