当前位置:网站首页>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;
}```
边栏推荐
猜你喜欢
随机推荐
Unix programming project - the client based on raspberry pie regularly obtains the temperature and reports it to the server
User and group management
KMP
UNIX实现IO多路复用之使用select函数实现网络socket服务端
编程入门2——找最小值
SSH服务
机器视觉学习总结
源码编译安装LAMP
两个及其简单的TCPUDP程序,树莓派与pc间的通信
Detailed explanation of three types of high-frequency function operation of basic C programming under liinux class I - file operation function (f)
Basic process of visitors
The difference between get request and post request and packet capturing
读《卓有成效的管理者-德鲁克》
Firewall knowledge, principle, equipment, manufacturer research summary report
Comparable, comparator of collections
LC:剑指 Offer 10- I. 斐波那契数列
RAID磁盘阵列
指针学习日记(三)
数据标注学习总结
Disk management operations








