题解 | 位操作练习(有更优)
位操作练习
https://www.nowcoder.com/practice/7bdc346ca39841f6a05f73d98477621d
/**
答案有更牛逼的思路,但我没来看。。。有空去看看
*/
#include <iostream>
using namespace std;
#define LEN 16
string to16Bin(int n, int nums) {
string res = "";
int cnt = 0;
while (n) {
if (n%2==1) {
res = "1" + res;
nums++;
}
else res = "0" + res;
n /= 2;
cnt++;
}
while (cnt++<LEN) res="0"+res;
return res;
}
string judge(string s1, string s2) {
for (int i = 0; i<15; i++) {
s1 = s1[LEN-1] + s1.substr(0,LEN-1);
if (s1 == s2) return "YES";
}
return "NO";
}
int main() {
int a, b;
while (cin>>a>>b) {
if (a==b) {
cout << "YES" << endl;
break;
}
int s1_cnt = 0, s2_cnt = 0;
string s1 = to16Bin(a, s1_cnt);
string s2 = to16Bin(b, s2_cnt);
// cout << s1 << " " << s2 << endl;
if(s1_cnt!=s2_cnt) cout << "NO" << endl;
else cout << judge(s1, s2) << endl;
}
}
查看29道真题和解析
韶音科技公司氛围 663人发布