zoukankan      html  css  js  c++  java
  • 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
    ", ans);
    17     }
    18     return 0;
    19 }
  • 相关阅读:
    c++数据类型
    c++注释
    c++基本语法
    c++环境配置
    c++简介
    9 HTTP和HTTPS
    12 RESTful架构(SOAP,RPC)
    剑指10二进制中1的个数
    第2章 新手必须掌握的Linux命令
    11个炫酷的Linux终端命令大全
  • 原文地址:https://www.cnblogs.com/shijianming/p/4140855.html
Copyright © 2011-2022 走看看