Math 之 hdu 1210

考试结束了,感觉好久没碰代码了,用一道简单数学题开始吧。。。

//  [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
*/
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int n;
 9     while (~scanf("%d", &n)) {
10         int pos = 1, ans = 0;
11         do{
12             if (pos <= n) pos = 2 * pos;
13             else pos = 2 * (pos - n) - 1;
14             ans++;
15         } while (pos != 1);
16         printf("%d\n", ans);
17     }
18     return 0;
19 }
posted @ 2014-04-28 19:41  JmingS  阅读(108)  评论(0编辑  收藏  举报