| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:直接输答案过了……没有想到更好的办法,唯有搜索了。用别的办法的人能否讲一下算法?In Reply To:直接输答案过了……没有想到更好的办法,唯有搜索了。用别的办法的人能否讲一下算法? Posted by:carlis at 2003-11-16 14:46:12 竟然能过
#include <iostream>
using namespace std;
int Josephus(int,long,int);
bool ok(int ,long );
int main()
{
int M;
long ans[14];
long sample;
for(M=1;M<14;M++)
{
sample=1;
while(!ok(M,sample))
sample++;
ans[M]=sample;
}
cin>>M;
while(M)
{ cout<<ans[M]<<endl;
cin>>M;}
return 0;
}
int Josephus(int n,long k,int i)
{
if(i==1)
return (k-1)%n;
else
return (k+Josephus(n-1,k,i-1))%n;
}
bool ok(int M,long sample)
{
int i;
for(i=1;i<=M;i++)
if(Josephus(2*M,sample,i)<M)
return false;
return true;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator