简体   繁体   English

有人可以帮我指出我的代码输出错误的地方吗?

[英]Can Someone help me point out where my code is going wrong with the output?

#include <iostream>
#include <vector>

using namespace std;

/*

Sample Input: 
2 2 ---------> Number of Arrays, Number of commands
3 1 5 4 -----> length of array, elements to add
5 1 2 8 9 3 -> length of array, elements to add

0 1 ---------> Command 1, row and column (first element of main vector, second element)
1 3 ---------> Command 2, row and column (second element of main vector, fourth element)

*/

int main()
{ //taking input of n and q.
    int n, q;
    cin >> n >> q;

    //make a main array to maintain sub arrays within and use queries on it.
    vector < vector<int> > main_vector;

    //make a sub vector and input it's value's using for loop
    vector <int> sub_vector;

    //declaring a variable to take input and keep pushing into sub_vector
    int input_element;

    //take input length of each vector in for loop
    int length_of_sub_vector;

    // now take n vectors input :
    for(int x = 0; x < n; x++)
    {
        //taking input length
        cin >> length_of_sub_vector;
        for(;length_of_sub_vector > 0; length_of_sub_vector--)
        {
            cin >> input_element;
            sub_vector.push_back(input_element);
        }
        main_vector.push_back(sub_vector);
    }
    
    //variable t and y for row and column
    int t, y;
    vector <int> to_print; 

    for(int p = 0; p < q; p++) //take input of the q following queries
    {
        cin >> t >> y;
        to_print.push_back(main_vector[t][y]);
    }

    for(int u = 0; u < to_print.size(); u++)
    {
        cout << to_print[u] << endl;
    }

}

The original Problem is over here : https://www.hackerrank.com/challenges/variable-sized-arrays/problem原来的问题在这里: https : //www.hackerrank.com/challenges/variable-sized-arrays/problem

I know there must be a better way to solve this question but I would like to learn what part of my code is leading to the undesired output, Thanks in Advance.我知道必须有更好的方法来解决这个问题,但我想了解我的代码的哪一部分导致了不需要的输出,在此先感谢。

Output Should be :输出应该是:

5
9

Output I'm getting :我得到的输出:

5
1

Live demo现场演示

You are missing a vector.clear() statement in your for loop that inserts values into the sub_vectors.您在 for 循环中缺少将值插入 sub_vectors 的 vector.clear() 语句。

#include <iostream>
#include <vector>

using namespace std;

/*

Sample Input: 
2 2 ---------> Number of Arrays, Number of commands
3 1 5 4 -----> length of array, elements to add
5 1 2 8 9 3 -> length of array, elements to add

0 1 ---------> Command 1, row and column (first element of main vector, second             element)
1 3 ---------> Command 2, row and column (second element of main vector, fourth element)

*/

int main()
{ //taking input of n and q.
    int n, q;
    cin >> n >> q;

    //make a main array to maintain sub arrays within and use queries on it.
    vector < vector<int> > main_vector;

    //make a sub vector and input it's value's using for loop
    vector <int> sub_vector;

    //declaring a variable to take input and keep pushing into sub_vector
    int input_element;

    //take input length of each vector in for loop
    int length_of_sub_vector;

    // now take n vectors input :
    for(int x = 0; x < n; x++)
    {
        sub_vector.clear();
        //taking input length
        cin >> length_of_sub_vector;
        for(;length_of_sub_vector > 0; length_of_sub_vector--)
        {
            cin >> input_element;
            sub_vector.push_back(input_element);
        }
        main_vector.push_back(sub_vector);
    }

    //variable t and y for row and column
    int t, y;
    vector <int> to_print; 

    for(int p = 0; p < q; p++) //take input of the q following queries
    {
        cin >> t >> y;
        to_print.push_back(main_vector[t][y]);
    }

    for(int u = 0; u < to_print.size(); u++)
    {
        cout << to_print[u] << endl;
    }

}

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

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