当前位置:网站首页>信息学奥赛一本通 1332:【例2-1】周末舞会
信息学奥赛一本通 1332:【例2-1】周末舞会
2022-06-27 10:51:00 【君义_noip】
【题目链接】
【题目考点】
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;
}
边栏推荐
- iMeta:高颜值绘图网站imageGP+视频教程合集,已被引360次(220625更新)
- Exception in Chinese character fuzzy query of MySQL database
- unity shadow 和outline组件动态加载出错解决方案、问题深入分析
- Oracle-分组统计查询
- 【TcaplusDB知识库】Tmonitor后台一键安装介绍(二)
- For a moment, the ban of the US e-cigarette giant has been postponed, and products can be sold in the US for the time being
- Openpyxl table reading instance
- 在外企远程办公是什么体验? | 社区征文
- The tutor invites you to continue your doctoral study with him. Will you agree immediately?
- Deep learning in finance in cross sectional sectional predictions for random forests
猜你喜欢

【TcaplusDB知识库】Tmonitor后台一键安装介绍(一)

浅析基于边缘计算的移动AR实现(中)

One copy ten, CVPR oral is accused of plagiarizing a lot

C语言学习-Day_06

KDD 2022 | 基于分层图扩散学习的癫痫波预测
![File name setting causes an error to be written to writelines: oserror: [errno 22] invalid argument](/img/08/2d4f425e6941af35616911672b6fed.png)
File name setting causes an error to be written to writelines: oserror: [errno 22] invalid argument

【TcaplusDB知识库】Tmonitor单机安装指引介绍(一)

go-zero微服务实战系列(七、请求量这么高该如何优化)

Learning notes - data set generation

KDD 2022 | epileptic wave prediction based on hierarchical graph diffusion learning
随机推荐
Exception in Chinese character fuzzy query of MySQL database
Imeta: a collection of imagegp+ video tutorials of high-value drawing websites, which has been cited 360 times (220625 updates)
堆-堆排序-TopK
Future & CompletionService
[tcapulusdb knowledge base] tcapulusdb machine initialization and launch introduction
【Methodot 专题】什么样的低代码平台更适合开发者?
Uniform Asymptotics by Alexei
在外企远程办公是什么体验? | 社区征文
Array object in JS
Feedforward feedback control system design (process control course design matlab/simulink)
红包雨: Redis 和 Lua 的奇妙邂逅
Based on swift admin's rapid background development framework, I made a rookie tutorial [professional version]
Win10 shortcut key sorting
浅析基于边缘计算的移动AR实现(中)
The tutor invites you to continue your doctoral study with him. Will you agree immediately?
Leetcode 729. 我的日程安排表 I(提供一种思路)
Glide缓存机制
LeetCode 522 最长特殊序列II[枚举 双指针] HERODING的LeetCode之路
VPT Model Video Explanation
Institute of Microbiology, Chinese Academy of Sciences recruited 20 young PI, with a resettlement fee of 2million yuan and a start-up fund of 10million yuan (long-term effective)