约瑟夫问题,感谢这篇博客,让我想了两天之后,看了n篇解题报告都没想懂的情况下,彻底搞懂了这个问题
blog.csdn.net/tsaid/article/details/7313382
#include <iostream> #include <cstdio> using namespace std; int f[15]; int solve(int n,int m) { int i,loc=0; for(i=1;i<=n;i++) { loc=(loc+m-1)%(2*n-i+1); if(loc<n) return 0; } return 1; } int main() { int i,j; for(i=1;i<=14;i++) { for(j=i+1;;j+=(i+1)) { if(solve(i,j)) { f[i]=j; break; } if(solve(i,j+1)) { f[i]=j+1; break; } } } int n; while(scanf("%d",&n)&&n) printf("%d\n",f[n]); return 0; }