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

 

 

转载于:https://www.cnblogs.com/forever4444/archive/2009/05/09/1453430.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值