高级农民
- 积分
- 1381
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2013-11-28
- 最后登录
- 1970-1-1
|
http://www.1point3acres.com/bbs/thread-142143-1-1.html 我的C++解法.
- #include <iostream>
- #include <queue>
- using namespace std;
- float roundRobin(int* Atime, int len, int* Etime, int q) {
- if (len < 2) return 0;
- queue< pair<int, int> > qq; // execution time, insert time.
- int currenttime = 0, stackhead = 0, currenttask = 0, totalwaittime = 0;
- bool killcurrenttask = 0;
- while (qq.size() || stackhead != len || killcurrenttask) {
- while (stackhead < len && Atime[stackhead] <= currenttime) {
- qq.push(make_pair(Etime[stackhead], Atime[stackhead]));
- stackhead++;
- }
- if (killcurrenttask) {
- qq.push(make_pair(currenttask - q, currenttime));
- }
- if (!qq.size())
- currenttime = Atime[stackhead];
- else {
- currenttask = qq.front().first;
- totalwaittime += currenttime - qq.front().second;
- qq.pop();
- killcurrenttask = currenttask > q;
- currenttime += ((q < currenttask) ? q : currenttask);
- }
- }
-
- return totalwaittime / (double)(len);
- }
- int main() {
- // int Atime[] = { 0, 1, 3, 9 };
- // int Etime[] = { 2, 1, 7, 5 };
- // int q = 2;
- // int Atime[] = { 0, 1, 4 };
- // int Etime[] = { 5, 2, 3 };
- // int q = 3;
- int Atime[] = { 0, 4, 8 };
- int Etime[] = { 1, 1, 1 };
- int q = 3;
- cout << roundRobin(Atime, sizeof(Atime) / sizeof(int), Etime, q);
- // your code goes here
- return 0;
- }
复制代码 |
|