编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间
#5953 #1060. 海明码 Compile Error 0 0 ms 0 K C++ 17 / 2.3 K AllenYGY 2024-08-17 17:57:58
显示原始代码

#include <bits/stdc++.h>
#include <iostream>
#define int long long

#define endl '\n'

using namespace std;

int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    return x * f;
}

void write(int x) {
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

void write(int x) {
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

int checkHammingcode(const string& data, const string& parity) {
    int size = parity.size();
    vector<int> check(size, 0);

    for (int i = 0; i < data.size(); i++) {
        if (data[i] == '1') {
            int position = i + 1;
            for (int j = 0; j < size; j++) {
                if (position & (1 << j)) {
                    check[j]++;
                }
            }
        }
    }

    for (int i = 0; i < size; i++) {
        if (parity[i] == '1') {
            check[i]++;
        }
    }

    int error_position = 0;
    for (int i = size - 1; i >= 0; i--) {
        if (check[i] % 2 != 0) {
            error_position |= (1 << i);
        }
    }

    return error_position;
}

signed main() {
    int n = read();
    string s;
    cin >> s;
    string data = s.substr(0, n);
    string parity = s.substr(n, s.size() - n - 1);
    int parity_check = s[n - 1] - '0';
    for (char bit : data) {
        parity_check ^= (bit - '0');
    }

    if (parity_check == 0) {
        cout << data << endl;
    } else {
        int error_position = checkHammingcode(data, parity);

        if (error_position > 0 && error_position <= n) {
            data[error_position - 1] = (data[error_position - 1] == '0') ? '1' : '0';
        }

        cout << data << endl;
    }

    return 0;
}

编译信息

/sandbox/1/a.cpp:31:5: error: redefinition of 'long long int read()'
   31 | int read(){
      |     ^~~~
/sandbox/1/a.cpp:8:5: note: 'long long int read()' previously defined here
    8 | int read(){
      |     ^~~~
/sandbox/1/a.cpp:45:6: error: redefinition of 'void write(long long int)'
   45 | void write(int x){
      |      ^~~~~
/sandbox/1/a.cpp:21:6: note: 'void write(long long int)' previously defined here
   21 | void write(int x){
      |      ^~~~~