【题目链接】
【题目考点】
1. 队列
【解题思路】
用两个队列模拟男女两队人,先让两队的人入队。
舞曲数目为n。
每次循环让两队分别出队1人,这两人配对跳舞,输出这两个人的编号。
而后出队的这两个人分别入队到队尾,回到自己的队列中。
如此循环n次。
【题解代码】
解法1:用数组与表达式实现队列
#include <bits/stdc++.h>
using namespace std;
#define N 1005
int que_m[N], que_f[N];//两个队列
int h_m, t_m, h_f, t_f;//两个队列的头尾指针
int main()
{
int m, f, temp_m, temp_f, n;//m,f:男女人数 temp_m, temp_f:临时出队的男女编号 n:舞曲数
cin >> m >> f >> n;
for(int i = 1; i <= m; ++i)//男士入队
que_m[++t_m] = i;
for(int i = 1; i <= f; ++i)//女士入队
que_f[++t_f] = i;
for(int i = 1; i <= n; ++i)
{
temp_m = que_m[++h_m];//男队,女队各出队1人
temp_f = que_f[++h_f];
cout << temp_m << ' ' << temp_f << endl;
que_m[++t_m] = temp_m;//把两个出队的人再入队
que_f[++t_f] = temp_f;
}
return 0;
}
解法2:使用C++ STL
#include <bits/stdc++.h>
using namespace std;
int main()
{
queue<int> q1, q2;//q1:男士队列 q2:女士队列
int a, b, n;
cin >> a >> b >> n;
for(int i = 1; i <= a; ++i)
q1.push(i);
for(int i = 1; i <= b; ++i)
q2.push(i);
for(int i = 1; i <= n; ++i)
{
cout << q1.front() << ' ' << q2.front() << endl;//两队队头出来配对跳舞
q1.push(q1.front());//将队头的值入队到队尾
q1.pop();//队头出队
q2.push(q2.front());
q2.pop();
}
return 0;
}