zoukankan      html  css  js  c++  java
  • toj 2841 Bitwise Reverse

    2841.   Bitwise Reverse
    Time Limit: 1.0 Seconds   Memory Limit: 65536K
    Total Runs: 1520   Accepted Runs: 1025



    Professor Robby invents a powerful encryption method, but he is too lazy to implement it. So he turns to you for help.

    In fact, the encryption method is only applied to positive integers. At first, we express the number as binary code, that is, a string only contain '0' and '1', and the first digit can't be '0'. Then we reverse the string. And the last step, we calculate the reversed binary code and express it in decimal again.

    For example, we want to encrypt the number 14, we express it as 1110, after reversing it we get 0111, and (0111)2 = 7. So we get 7.

    Input

    There is only one line for each test case, containing the positive integer to be encrypted. You can assume the number is not more than 106.

    The input is terminated with a zero.

    Output

    Output one line for each test case, indicating the number after encryption.

    Sample Input

    5
    6
    14
    0
    

     

    Sample Output

    5
    3
    7
    

     

    Problem Setter: RoBa



    Source: Tianjin Metropolitan Collegiate Programming Contest 2007


    Submit   List    Runs   Forum   Statistics
                            Show Code - Run ID 633955
    Submit Time: 
    2009-05-09 17:10:25     Language: GNU C++     Result: Accepted
        Pid: 
    2841     Time: 0.00 sec.     Memory: 1208 K.     Code Length: 0.4 K.

    --------------------------------------------------------------------------------

    #include 
    <iostream>
    #include 
    <cmath>
    #include 
    <stack>
    using namespace std;
    stack
    <int>S;
    int main()
    {
        
    int n,sum;
        
    while(scanf("%d",&n)!=EOF&&n!=0)
        {
            sum
    =0;
            
    int i=0;
            
    while(!S.empty())
                S.pop();
            
    while(n!=0)
            {
                
    int temp=n%2;
                n
    =n/2;
                S.push(temp);
            }
            
    while(!S.empty())
            {
                
    int temp2=S.top();
                S.pop();
                sum
    +=(int)(temp2*(pow(2.0,i++)));
            }
            printf(
    "%d\n",sum);
        }
        
    return 0;
    }

    --------------------------------------------------------------------------------

    Tianjin University Online Judge v1.
    2.4
  • 相关阅读:
    自定义主键自增规则 Oracle Mssql(全库唯一标识)
    SQL Server 字符串 参数详解
    js 实现类似C# 委托的那种效果
    网页页面“返回到顶部”
    js substr与substring的区别
    js 格式化时间/Date(1425027069000)/格式化为yyyy-MM-dd HH:mm:ss
    js 设置一个按周期向服务器发送请求 推荐是用 setTimeOut函数
    jquery 几个常用的选择器
    Oracle
    小议出参入参
  • 原文地址:https://www.cnblogs.com/forever4444/p/1453430.html
Copyright © 2011-2022 走看看