编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间
#5730 #1060. 海明码 Compile Error 0 0 ms 0 K C++ 17 / 1.4 K s230004040 2024-08-17 14:48:01
显示原始代码
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main() {
    int n;
    cin >> n;  // 读取二进制数的长度

    string hammingCode;
    cin >> hammingCode;  // 读取Hamming码

    int k = log2(n + 1);  // 计算校验位的数量
    vector<int> positions;

    // 计算需要校验的位的位置
    for (int i = 0; i < k; ++i) {
        positions.push_back((1 << i) - 1);
    }

    int errorPosition = 0;

    // 检查每个校验位
    for (int i = 0; i < k; ++i) {
        int parity = 0;
        int step = 1 << i;

        // 计算对应校验位的奇偶性
        for (int j = positions[i]; j < n; j += step * 2) {
            for (int l = j; l < j + step && l < n; ++l) {
                parity ^= (hammingCode[l] - '0');
            }
        }

        // 如果校验位不匹配,记录错误位置
        if (parity != 0) {
            errorPosition += (1 << i);
        }
    }

    // 如果有错误,修正错误位
    if (errorPosition > 0 && errorPosition <= n) {
        hammingCode[errorPosition - 1] = (hammingCode[errorPosition - 1] == '0') ? '1' : '0';
    }

    // 输出去除校验位后的结果
    for (int i = 0; i < n; ++i) {
        if (!binary_search(positions.begin(), positions.end(), i)) {
            cout << hammingCode[i];
        }
    }
    cout << endl;

    return 0;
}

编译信息

/sandbox/1/a.cpp: In function 'int main()':
/sandbox/1/a.cpp:49:14: error: 'binary_search' was not declared in this scope
   49 |         if (!binary_search(positions.begin(), positions.end(), i)) {
      |              ^~~~~~~~~~~~~