题解 | #奇偶校验#
奇偶校验
https://www.nowcoder.com/practice/729d11d043d8448e83befa1410b694fe
#include "bits/stdc++.h" using namespace std; void getAns(string msg) { for (int i = 0; i < msg.length(); ++i) { char c = msg[i]; string str_c; int sum_one = 0; while (c) { int temp = c & 1; str_c += (char)(temp + '0'); if (temp) sum_one++; c >>= 1; } while (str_c.length() < 7) str_c += '0'; str_c += sum_one & 1 ? "0" : "1"; std::reverse(str_c.begin(), str_c.end()); cout << str_c << endl; } } int main() { string msg; while (cin >> msg) { getAns(msg); } }
需要注意的是每个字符占位8bit,转换为二进制后如果位数不足8位需要补全