3288.Stockholm Numbers(注意结论)

题目链接:http://acm.tju.edu.cn/toj/showp3288.html


3288.    Stockholm Numbers
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 421    Accepted Runs: 238



Stockholm Numbers are positive integers which when we write it in binary it has even digits 1. Can you tell me the  K-th Stockholm Number?

Input

The first line of input is the number of test case. For each test case, There is only one line contains only one integer  K. There is a blank line before each test case.

1 ≤ K ≤ 109

Output

For each test case output the answer on a single line.

Sample Input

3

1

2

3

Sample Output

3
5
6

Author: windy7926778



Source: Sichuan University Programming Contest 2009
Submit   List    Runs   Forum   Statistics


这道题有个结论就是:

如果k含有偶数个1,即k是这种数,那么第k个这种数是它的2倍;否则,是k的2倍+1

知道这个结论就很好写了:


#include<stdio.h>
using namespace std;
int main(){
    int num;
    scanf("%d",&num);
    while(num--){
        int cast;
        int count = 0;
        scanf("%d",&cast);
        int k=cast;
        while(cast){
            if(cast%2==1)
                count++;
            cast/=2;
        }
        printf("%d\n",count%2?2*k+1:2*k);
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值