学会打表。。。
其实程序计算也挺快的,left=n-i; j=(m-1)%left+1;这两句加上去1s内就把表打好了。
经典约瑟夫问题,直接用数组做的。
#include <iostream> #include <string.h> using namespace std; int h[40]; int check(int n,int m,int k) { memset(h,0,sizeof(h)); int i,j,now=n-1,left; for (i=0;i<k;i++){ left=n-i; j=(m-1)%left+1; while (j) { now=(now+1)%n; if (h[now]==0) j--; } // cout<<now+1<<endl; if (now<k) return 0; h[now]=1; } return 1; } int ans[]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881}; int main(){ int n,d,i,m,k; while (cin>>k,k){ cout<<ans[k]<<endl; } /* for (k=1;k<=13;k++){ n=2*k; m=k; // check(6,5,3);return 0; while (check(n,m,k)==0) m++; cout<<m<<","; } */ return 0; }