题目链接:http://acm.tju.edu.cn/toj/showp3288.html
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
这道题有个结论就是:
如果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);
}
}