今天做星宸科技编程最后一道,始终只通过50%。求助。
题目大概是,输入两个uint16的整数,如果其中一个uint16的整数,与另一个数的uint32二进制表示形式通过循环左移若干位后相等,则返回true,否则返回false;
给的示例
输入:2 4
输出:true
我代码是
#include<bits/stdc++.h> using namespace std; typedef unsigned short int uint16; typedef unsigned int uint32; uint32 bit_move(uint32 val, int n) { uint32 size = sizeof(val) * 8; n = n % size; //return (val >> (size - n) | (val << n)); return (val << (size - n) | (val >> n)); } bool iszhuanhuan(uint16 a1, uint16 b1) { uint16 a = a1; uint32 b = (uint32)b1; if (a == b) return true; else for (int i = 1; i < 32; i++) { b = bit_move(b, i); if (a == b) { return true; } } return false; } int main() { uint16 a1, b1; scanf("%hd%hd", &a1, &b1);//输入两个数 if (iszhuanhuan(a1, b1) || iszhuanhuan(b1, a1)) cout << "true" << endl; else cout << "fasle" << endl; return 0; }
全部评论
(1) 回帖