ALDS1_3_B-Queue(队列)

There are n processes in a queue. Each process has namei and timei. The round-robin scheduling handles the processes in order. A round-robin scheduler gives each process a quantum (a time slot) and interrupts the process if it is not completed by then. The process is resumed and moved to the end of the queue, then the scheduler handles the next process in the queue.

For example, we have the following queue with the quantum of 100ms.

A(150) - B(80) - C(200) - D(200)
First, process A is handled for 100ms, then the process is moved to the end of the queue with the remaining time (50ms).

B(80) - C(200) - D(200) - A(50)
Next, process B is handled for 80ms. The process is completed with the time stamp of 180ms and removed from the queue.

C(200) - D(200) - A(50)
Your task is to write a program which simulates the round-robin scheduling.
Input
n q
name1 time1
name2 time2

namen timen
In the first line the number of processes n and the quantum q are given separated by a single space.

In the following n lines, names and times for the n processes are given. namei and timei are separated by a single space.

Output
For each process, prints its name and the time the process finished in order.

Constraints
1 ≤ n ≤ 100000
1 ≤ q ≤ 1000
1 ≤ timei ≤ 50000
1 ≤ length of namei ≤ 10
1 ≤ Sum of timei ≤ 1000000
Sample Input 1
5 100
p1 150
p2 80
p3 200
p4 350
p5 20
Sample Output 1
p2 180
p5 400
p1 450
p3 550
p4 800
Notes
Template in C


/*利用STL的queue类*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

const int maxx = 100000 + 10;
struct task {
    // 任务的名字和耗费的时间
    char name[100];
    int t;
};
queue<task> Queue;

int main() {
    int n, q;
    scanf("%d %d", &n, &q);
    for (int i = 0; i < n; i++) {
        task t;
        scanf("%s %d", t.name, &t.t);
        Queue.push(t);
    }
    // 当前时间
    int nowtime = 0;
    while (!Queue.empty()) {
        // 取出队头
        task u = Queue.front();
        Queue.pop();
        // 执行时间片 p 和 所需要时间u.t
        int c = min(q, u.t);
        // 任务执行了 c ms
        u.t -= c;
        // 当前时间加上c ms
        nowtime += c;
        // 任务还没做完,加入到队尾
        if (u.t > 0) {
            Queue.push(u);
        }
        else { // 任务已经完成,打印出结果
            printf("%s %d\n", u.name, nowtime);
        }

    }
    return 0;
}
---------------------------------
/*实现队列功能*/
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

const int maxx = 100000 + 10;
struct task {
    // 任务的名字和耗费的时间
    char name[100];
    int t;
};
task Queue[maxx];

// 指向队头和队尾的指针
int head, tail;


// 判断循环队列是否已满
bool isFull() {
    return head == (tail + 1) % maxx;
}

// 判断循环队列是否为空
bool isEmpty() {
    return head == tail;
}

// 将元素x添加到队列的末尾
void enqueue(task x) {
    Queue[tail] = x;
    // 循环队列
    tail = (tail + 1) % maxx;
}

// 返回队头元素并删除
task dequeue() {
    task x = Queue[head];
    // 循环队列
    head = (head + 1) % maxx;
    return x;
}
int main() {
    int n, q;
    scanf("%d %d", &n, &q);
    for (int i = 0; i < n; i++) {
        scanf("%s %d", Queue[i].name, &Queue[i].t);
    }
    // 当前已经有n个元素
    head = 0; tail = n;
    // 当前时间
    int nowtime = 0;
    while (!isEmpty()) {
        // 取出队头
        task u = dequeue();
        // 执行时间片 p 和 所需要时间u.t
        int c = min(q, u.t);
        // 任务执行了 c ms
        u.t -= c;
        // 当前时间加上c ms
        nowtime += c;
        // 任务还没做完,加入到队尾
        if (u.t > 0) {
            enqueue(u);
        }
        else { // 任务已经完成,打印出结果
            printf("%s %d\n", u.name, nowtime);
        }

    }
    return 0;
}

发布了430 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zqhf123/article/details/105552530