今天的计算机课上,老师给同学们出了一道题:
输入n个数,请将其中的偶数的二进制反转。
eg:输入1 6 5
其中6是偶数,二进制表示为110,反转后为011,代表3,所以最终输出1 3 5.
小贱君最近脑子不怎么好使,想了半天也没想出来如何做,最后他向你寻求帮助了,帮帮可怜的小贱君吧!
题意,把所有有数的二进制反转输出
位运算基础
>>
右移动一位<<
左移动一位x|=(1<<id)
将x的第id位设置成1
x>>id&1
判断x的第id
位是0
还是1
对于java
而言
- 右移应该使用无符号右移
>>>
- 左移和无符号左移是同一个符号
<<
#define debug
#ifdef debug
#include <time.h>
#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)
using namespace std;
#define show(x...) \
do { \
cout << "\033[31;1m " << #x << " -> "; \
err(x); \
} while (0)
void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
namespace FastIO{
char print_f[105];
void read() {}
void print() { putchar('\n'); }
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth) {
x = 0;
char ch = getchar();
ll f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
read(oth...);
}
template <typename T, typename... T2>
inline void print(T x, T2... oth) {
ll p3=-1;
if(x<0) putchar('-'), x=-x;
do{
print_f[++p3] = x%10 + 48;
} while(x/=10);
while(p3>=0) putchar(print_f[p3--]);
putchar(' ');
print(oth...);
}
} // namespace FastIO
using FastIO::print;
using FastIO::read;
int n, m, Q, K;
#define set_one(x, id) (x |= (1<<id))
signed main() {
#ifdef debug
freopen("test.txt", "r", stdin);
clock_t stime = clock();
#endif
while(~scanf("%d ", &n)) {
int p = 0, x;
while(n--) {
if(p++) printf(" ");
scanf("%d ", &x);
if((x&1)) {
printf("%d", x);
continue;
}
int max_id = 0, tmp = x;
while(tmp) { //判断x的最高位1是第几位
max_id ++;
tmp >>= 1;
}
int ans = 0, id = max_id - 1;
if(max_id == 1) { //如果只有1位,那必然是1,输出即可
ans = x;
} else {
for(int i=0; i<max_id; i++, id--) { //逆着扫一遍对所有二进制位为1的位置设置为1
if((x>>i&1)) //判断第i位是否为1
ans |= (1<<id); //设置id位为1
}
}
printf("%d", ans);
}
//111010000111
//111010000111
//111000010111
printf("\n");
}
#ifdef debug
clock_t etime = clock();
printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif
return 0;
}
java代码也是一样的
// package FileIO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
// FileInputStream fis = new FileInputStream("C:\\Users\\majiao\\Desktop\\test");
// System.setIn(fis);
Scanner scan = new Scanner(System.in);
while(scan.hasNext()) {
int n = scan.nextInt();
int x, cnt = 0, p = 0;
while(n-- > 0) {
x = scan.nextInt();
cnt = 0;
if(p ++ > 0) System.out.print(" ");
if(1 == (x&1)) {
System.out.print(x);
continue;
} else {
int tmp = x;
while(tmp > 0) {
cnt ++;
tmp >>>= 1;
}
if(cnt == 1) {
System.out.print(x);
continue;
}
int id = cnt - 1, ans = 0;
for(int i=0; i<cnt; i++, id--) {
if(1 == (x>>>i&1)) {
ans |= (1 << id);
}
}
System.out.print(ans);
}
}
System.out.println();
}
}
}