使用队列解决报数问题

This commit is contained in:
LittleSheep 2024-05-19 15:21:35 +08:00
parent 7650e8edfb
commit 04fa87a8da

35
reporting-number/main.cc Normal file
View File

@ -0,0 +1,35 @@
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
queue<int> q;
int X, Y, K;
cin >> X >> Y >> K;
for (int i = 1; i <= X + Y; i++) {
q.push(i);
}
int count = 0;
while (q.size() > X) {
count++;
if (count == K) {
q.pop();
count = 0;
} else {
auto pin = q.front();
q.pop();
q.push(pin);
}
};
vector<int> v;
while (q.size() > 0) {
v.push_back(q.front());
q.pop();
}
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++) {
printf("%d ", v[i]);
}
printf("\n");
}