2025年北京邮电大学计算机考研复试机试真题
2025年北京邮电大学计算机考研复试上机真题
历年北京邮电大学计算机考研复试上机真题
历年北京邮电大学计算机考研复试机试真题
更多学校题目开源地址:https://gitcode.com/verticallimit1/noobdream
N 诺 DreamJudge 题库:输入 “学校名称” 即可筛选该校历年机试真题,题目均在考纲范围内,按难度自动排序。还可搭配《计算机考研机试攻略》刷题,书中题目可通过题号直接在题库中查找。
二进制数字翻转
题目描述
Time Limit: 1000 ms
Memory Limit: 256 mb
输入数据组数t
每组数据输入一个十进制数x(0<x<2^32),将其二进制位反转(共32位),然后输出对应的十进制数
输入输出格式
输入描述:
如题
输出描述:
如题
输入输出样例
输入样例#:
2 2 3
输出样例#:
1073741824 3221225472
代码一
- import java.util.*;
- public class Main{
- public static void main(String[] args){
- Scanner sc=new Scanner(System.in);
- int t=sc.nextInt();
- while(t>0){
- long n=sc.nextLong();
- System.out.println(reverse(n));
- t--;
- }
- sc.close();
- }
- public static long reverse(long x){
- long res=0;
- for(int i=1;i<=32;i++){
- res <<=1;
- res=res|(x&1);
- x >>= 1;
- }
- return res;
- }
- }
代码二
- #include <iostream>
- #include <cstdint> // 包含 uint32_t
- // 函数接收一个32位无符号整数,返回其位反转后的结果
- uint32_t reverseBits(uint32_t n) {
- uint32_t result = 0;
- for (int i = 0; i < 32; ++i) {
- // 1. 将 result 左移一位,为下一个比特位腾出空间
- result <<= 1;
- // 2. 检查 n 的最低位是否为 1
- if (n & 1) {
- // 3. 如果是 1,则将 result 的最低位也置为 1
- result |= 1;
- }
- // 4. 将 n 右移一位,以便在下一次循环中检查下一个比特位
- n >>= 1;
- }
- return result;
- }
- int main() {
- int t;
- std::cin >> t;
- while (t--) {
- uint32_t n;
- std::cin >> n;
- std::cout << reverseBits(n) << std::endl;
- }
- return 0;
- }
代码三
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n;
- cin >>n;
- while(n--){
- long long a;
- cin >>a;
- vector<int> b(32,0);
- int i=31;
- while(a){
- b[i]=a%2;
- a/=2;
- i--;
- }
- long long c=0;
- for(int j=0;j<32;j++){
- if(b[j]==1)
- c+=pow(2,j);
- }
- cout<<c<<endl;
- }
- return 0;
- }