问题简介:
小朋友围城一圈,从1号开始报数,报到M的那位同学出局,然后下一位同学报1,循环上述过程直到所有同学出局。
输出 依次出局的小朋友的号码
解题思路:
没什么好说的,就是模拟,出局我们可以不用真的删除,打个标记就行
废话:
先想清楚什么时候打标记再做,不要急着动手。
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,m;cin>>n>>m;
if(!n)return 0;
int count=n;
int poi=0;
int res[n+10];
memset(res,0,sizeof(res));
while(1){
for(int i=0;i<m-1;i++){
poi=(poi+1)%n;
while(res[poi])poi=(poi+1)%n;
res[poi]=1;
cout<<(poi+1)<<" ";
poi=(poi+1)%n;
count--;
if(!count){
cout<<endl;
exit(0);}
while(res[poi])poi=(poi+1)%n;
}
return 0;
}
标签:count,洛谷,int,res,出局,约瑟夫,while,P1996,poi From: https://blog.51cto.com/u_15910522/5931533