当前位置:网站首页>Zstuacm summer camp flag bearer
Zstuacm summer camp flag bearer
2022-07-23 06:03:00 【Rain and cold at night in Xiaoxiang】
Title Description
Annual Jiangsu “ Information and the future ” The summer camp for primary school students started again . Same as every year , The organizer also designed and arranged many interesting activities , The first one is still to choose the flag bearer of this summer camp ! Because this is a very honorary role , So there are still a lot of campers who sign up for the flag bearer competition in the summer camp , The operating Committee then stipulated : take N People in a row , Number 1~N. From 1 People start 1~M Forward counting , To report for duty M The people of , Continue from the next person 1 To M Number off 、 List out .( Be careful : When you count off in a certain direction and report to the tail , Continue counting in the opposite direction ). Go on like this , Until there is one left , This man is the flag bearer of this summer camp . Xiao Ming hopes very much to become a flag bearer , Can you compile a program to help him realize his wish ? If you can , Your program should output Xiao Ming's number .
Input
Include two integers N,M (2≤N,M≤300,N≥ M ), Separate... With a space
Output
Include an integer , Indicates Xiao Ming's number in the queue .
The sample input Copy
【 sample input 1】
9 3
【 sample input 2】
8 3
Sample output Copy
【 sample output 1】
8
【 sample output 2】
8
Tips
This problem can be achieved by using a two-way linked list
I prefer to use stl To complete the linked list , After all, it's convenient , The only thing to pay attention to is not to use cin and cout
Use faster scanf and printf
The point to pay attention to is the position to the beginning and end .
Be careful :!list.end(), What is returned is a garbled code ( Because the default value of linked list creation is an empty value )
but list.begin() What is returned is the first value entered
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<list>
using namespace std;
list<int>stu;
int main()
{
int n_total = 0;
scanf("%d", &n_total);
int m = 0;
scanf("%d", &m);
for (int i = 0; i < n_total; i++)
{
stu.push_back(i + 1);
}
int j = 1;
bool flag = true;
list<int>::iterator it=stu.begin();
list<int>::iterator it1 = it;
while (stu.size() != 1)
{
if (flag == true)
{
if (j != m)
{
it++;
if (it == stu.end())
{
it--;// Go back to the last element
flag = false;
continue;// At this point, you need to traverse from back to front
}
else
{
j++;
}
}
if (j == m)
{
j = 1;
it1 = it;
it++;// Take advantage of the situation and walk back one
stu.erase(it1);// Delete the data in this location
}
if (it == stu.end())
{
it--;// The last element
flag = false;
continue;
}
}
else
{
if (j!=m)
{
j = j + 1;
it--;
}
if (j == m)
{
j = 1;
it1 = it;
if (it == stu.begin())// If it happens to be the beginning, count off
{
it++;
flag = true;
stu.erase(it1);// Delete the beginning
continue;
}
it--;
stu.erase(it1);// Delete it1
}
if (it == stu.begin())
{
flag = true;
continue;
}
}
}
printf("%d\n", *it);
return 0;
}```
边栏推荐
猜你喜欢

Fiddler Script个性化配置显示

shell基本命令

Common problems of multiple processes - how to lock the same parent thread variable (critical resource) when creating multiple threads so that the shared parent thread variable is not repeatedly modif

The difference between get request and post request and packet capturing

APUE process deeply understands fork and EXECL - obtain virtual machine IP address in program code

Link: fatal error lnk1104: unable to open the file "opencv_world340.lib"

Shell basic commands

03. Design of large-scale high parallel micro service system

防火墙粗略了解

DHCP原理与配置
随机推荐
Day 3 summary & homework
LIinux下的基本C编程的三类高频函数操作详解第一类——文件操作函数(f)
数值变量的运算及特殊变量
Fiddler Script个性化配置显示
RAID disk array
Jmeter上传和下载文件
[JMeter]响应内容中文乱码解决办法
移动应用分类
shell之条件语句
Link: fatal error lnk1104: unable to open the file "opencv_world340.lib"
Asset mapping process
vector与迭代器部分内容学习
两个及其简单的TCPUDP程序,树莓派与pc间的通信
SNAT与DNAT
逻辑卷管理
防火墙粗略了解
CSDN陪伴了我四年本科生活,自己也开始好好记录一下啦
Jmeter正则表达式提取器
防火墙调研报告
UNIX编程—网络socket