简体   繁体   English

奇怪 output C++

[英]Strange output C++

I was trying to solve a simple question on a coding site.我试图在编码站点上解决一个简单的问题。 I must find how many pairs are there in an array that sumed up are divisible by a given integer k.我必须找到一个数组中有多少对可以被给定的 integer k 整除。 The logic in the code below is bad, I've got 100p afterwards, but I found a strange bug in the bad code.下面代码中的逻辑很糟糕,后来我得到了 100p,但我在糟糕的代码中发现了一个奇怪的错误。

Here it is:这里是:

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

int divisibleSumPairs(int n, int k, vector<int> ar) {
    int modK[k] = {0};
    for(int i = 0; i < n; ++i)
        ++modK[ar[i] % k];
    int cnt = 0;
    ///cout << modK[0] << '\n';  <- If I uncomment this, the output is 1
    cnt += modK[0] * ((modK[0]) - 1) / 2;
    
    if(k % 2 == 0)
        cnt += (modK[k / 2] * (modK[k / 2] - 1)) / 2;
    else
        for(int i = 0; i < k / 2; ++i)
            cnt += modK[i] * modK[k - i];
        
    return cnt;
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string first_multiple_input_temp;
    getline(cin, first_multiple_input_temp);

    vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));

    int n = stoi(first_multiple_input[0]);

    int k = stoi(first_multiple_input[1]);

    string ar_temp_temp;
    getline(cin, ar_temp_temp);

    vector<string> ar_temp = split(rtrim(ar_temp_temp));

    vector<int> ar(n);

    for (int i = 0; i < n; i++) {
        int ar_item = stoi(ar_temp[i]);

        ar[i] = ar_item;
    }

    int result = divisibleSumPairs(n, k, ar);

    fout << result << "\n";

    fout.close();

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );

    return s;
}

vector<string> split(const string &str) {
    vector<string> tokens;

    string::size_type start = 0;
    string::size_type end = 0;

    while ((end = str.find(" ", start)) != string::npos) {
        tokens.push_back(str.substr(start, end - start));

        start = end + 1;
    }

    tokens.push_back(str.substr(start));

    return tokens;
}

If I comment out cout << modK[0] << '\n';如果我注释掉cout << modK[0] << '\n'; , the ouput (in an out file, not on the screen) is 65141, If I don't it is 1. Why? ,输出(在输出文件中,不在屏幕上)是 65141,如果我不这样做,它就是 1。为什么? This is the problem:这就是问题:

https://www.hackerrank.com/challenges/three-month-preparation-kit-divisible-sum-pairs/problem?h_l=interview&playlist_slugs%5B%5D=preparation-kits&playlist_slugs%5B%5D=three-month-preparation-kit&playlist_slugs%5B%5D=three-month-week-one https://www.hackerrank.com/challenges/three-month-preparation-kit-divisible-sum-pairs/problem?h_l=interview&playlist_slugs%5B%5D=preparation-kits&playlist_slugs%5B%5D=three-month-preparation- kit&playlist_slugs%5B%5D=三个月-一周-一

i为 0 时, modK[k - i]访问越界。这可能应该是modK[k - i - 1]

In C++, the size of an array must be a compile time constant .在 C++ 中,数组的大小必须是编译时常量 So you cannot write code like:所以你不能写这样的代码:

int n = 10;
int arr[n];    //incorrect

Correct way to write this would be:正确的写法是:

const int n = 10;
int arr[n];    //correct

For the same reason the following statement is incorrect in your code :出于同样的原因,以下语句在您的代码中不正确:

int modK[k] = {0}; //incorrect because k is a function parameter

Also take a look at: Why should I not #include <bits/stdc++.h>也看看: 为什么我不应该#include <bits/stdc++.h>

You can/should also use the debugger to check the value of the indices and see if you are trying to access out of bound element of the array.您还可以/应该使用调试器来检查索引的值,看看您是否正在尝试访问数组的越界元素。 This is a common reason for undefined behavior .这是未定义行为的常见原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM