Visitors hit counter dreamweaver

线性结构 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;
}
posted @ 2011-11-19 21:07  Jason Damon  阅读(189)  评论(0)    收藏  举报