当前位置:网站首页>信息学奥赛一本通 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;
}
边栏推荐
- Co jump
- [tcapulusdb knowledge base] Introduction to tmonitor background one click installation (I)
- [noodle classic] Yunze Technology
- Oracle trigger stored procedure writes at the same time
- Based on swift admin's rapid background development framework, I made a rookie tutorial [professional version]
- Analysis of mobile ar implementation based on edge computing (Part 2)
- Imeta: a collection of imagegp+ video tutorials of high-value drawing websites, which has been cited 360 times (220625 updates)
- User authentication technology
- Future & CompletionService
- [cloud enjoys freshness] community weekly · vol.68- Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + business realization
猜你喜欢

Oracle连接MySQL报错IM002
![leetcode:522. Longest special sequence II [greed + subsequence judgment]](/img/43/9b17e9cb5fee9d14c2986a2141889d.png)
leetcode:522. Longest special sequence II [greed + subsequence judgment]

Win10快捷键整理
![[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)](/img/6d/8b1ac734cd95fb29e576aa3eee1b33.png)
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)

KDD 2022 | 基于分层图扩散学习的癫痫波预测

Oracle-分组统计查询

在外企远程办公是什么体验? | 社区征文

微软云 (Microsoft Cloud) 技术概述

2-4 installation of Nessus under Kali
![[hcie-rs review mind map] - STP](/img/b5/b89e59fe7f23bf23feeadb991acba7.png)
[hcie-rs review mind map] - STP
随机推荐
torchvision. models._ utils. Intermediatelayergetter tutorial
Proe/Creo家电产品结构设计规范-电煎锅
Learning notes - data set generation
NAACL 2022 | TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
Oracle-分组统计查询
[tcapulusdb knowledge base] tcapulusdb Model Management Introduction
Working at home is more tiring than going to work at the company| Community essay solicitation
audiotrack与audioflinger
【TcaplusDB知识库】TcaplusDB机器初始化和上架介绍
Deep learning in finance in cross sectional sectional predictions for random forests
Oracle连接MySQL报错IM002
C any() and aii() methods
[hcie-rs review mind map] - STP
Co jump
基于swiftadmin极速后台开发框架,我制作了菜鸟教程[专业版]
NVME2.0协议——新特性
闭包的常见问题
Mongodb cross host database copy and common commands
Concepts of concurrency, parallelism, asynchronism, synchronization, multithreading and mutual exclusion
Win10 shortcut key sorting