考试结束了,感觉好久没碰代码了,用一道简单数学题开始吧。。。
// [4/28/2014 Sjm] /* (注: pos 代表 1 的位置) 分别画出 n = 1, 2, 3 分析即可推出: 1) 当 1 回到它原来位置时,则在经过M次洗牌后第一次重新得到初始时的顺序。 2) 当 pos <= n 时, pos = 2*n ; 3) 当 pos > n 时, pos = 2*(pos - (n+1)) + 1。 故可化为: pos = 2 * (pos - n) - 1 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
int n;
while (~scanf("%d", &n)) {
int pos = 1, ans = 0;
do{
if (pos <= n) pos = 2 * pos;
else pos = 2 * (pos - n) - 1;
ans++;
} while (pos != 1);
printf("%d\n", ans);
}
return 0;
}