线性结构 Joseph
N个人排成一圈,按顺时针从1到N编号。从1开始顺时针数到第m个人,让其出圈,从下一个人开始继续数,数到第m个人,让其出圈,重复上述过程然后从出圈者的后继位置开始数,重复上述过程,直到所有人都出圈。
n输入:N,m (1<=N,m<=30000)
n输出:出圈人的编号序列。
很简单的。。。
#include <iostream>
using namespace std;
int A[30001]={0};
int n,m,count=0,flag=0;//flag to count to the sum of the people out
void Joseph(){
for(int i=0;;i=(i+1)%n){
if(A[i]!=0)
continue;
count++;
if(count==m){
cout<<i+1<<"";
flag++;
count=0;
A[i]=1;
}
if(flag==n)
return;
}
}
int main(){
cin>>n>>m;
Joseph();
return 0;
}
我是一名在校大学生,热爱编程,虽然起步晚了些,但我会努力的。呵呵!
数据结构 算法

浙公网安备 33010602011771号