【问题标题】:c++ variable sized arrays from Hackerrank with vectors来自Hackerrank的带有向量的c ++可变大小数组
【发布时间】:2019-03-02 16:59:38
【问题描述】:

我想在 Hackerrank 上解决一个名为“可变大小数组”的挑战,我想使用向量来解决这个问题。我写的代码不能正常工作,我尝试调试它,但我一无所获。我将不胜感激 这是挑战:

考虑一个 n 元素数组 a,其中数组中的每个索引 i 都包含对整数数组的引用 Ki(其中 Ki 的值因数组而异)。有关图表,请参阅下面的说明部分。

给定 a,你必须回答 q 个问题。每个查询都采用 i j 格式,其中 i 表示数组中的索引,j 表示数组中位于 a[i] 的索引。对于每个查询,查找并打印数组中元素 j 的值,位于 a[i]a 新行的位置。

输入格式

第一行包含两个用空格分隔的整数,分别表示 n(可变长度数组的数量)和 q(查询的数量)的值。 后续行的每一行都包含一个空格分隔的序列,格式为 k a[i]0 a[i]1 … a[i]k-1,描述位于 a[i] 的 k 元素数组。 q 后续行中的每一行都包含两个以空格分隔的整数,分别描述查询的 i(数组 a 中的索引)和 j(a[i] 引用的数组中的索引)的值。

示例输入
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3

样本输出
5
9

这是我的代码(我将把它留给 couts 进行调试):

#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::vector;
using std::endl;

int main() {

    int numberOfQueries = 0;
    int numberOfArrays = 0;
    cout << "Enter Nr.Of Arrays followed by Nr.Of Queries:";
    cin >> numberOfArrays >> numberOfQueries;
    cout << "Nr.Of Arrays: " << numberOfArrays << endl;
    cout << "Nr.Of Queries: " << numberOfQueries << endl;
    vector<vector<int>>multiArray;
    cout << "MultiArray size: " << multiArray.size();
    while (numberOfArrays != 0) {
        int vsize = 0;
        cout << "\nenter array starting by its size: ";
        cin >> vsize;
        cout << " Size entered  is: " << vsize << endl;
        vector<int> vec1(vsize);
        cout << "Array Size is: " << vec1.size() << endl;
        //int element = 0;
        while (cin >> vsize) {
            cout << "Element is: " << vsize << "\n";
            vec1.push_back(vsize);
        };
        multiArray.push_back(vec1);
        numberOfArrays--;
        cout << "MultiArray size: " << multiArray.size();
        cout << "Nr.Of Arrays: " << numberOfArrays << endl;
    };
    while (numberOfQueries > 0) {
        int i = 0, j = 0;
        cout << "\nQuery indexes:";
        cin >> i >> j;
        cout << multiArray[i][j];
        numberOfQueries--;
    }
}

【问题讨论】:

  • 你认为while (cin &gt;&gt; vsize) 是做什么的?提示:它不会在按下回车键之前进行扫描

标签: c++ c++11 vector


【解决方案1】:
    while (cin >> vsize) {
        cout << "Element is: " << vsize << "\n";
        vec1.push_back(vsize);
    };

应该是这样的

    for (int i = 0; i < vsize; ++i) {
        int elem;
        cin >> elem;
        cout << "Element is: " << elem << "\n";
        vec1.push_back(elem);
    }

while (cin &gt;&gt; vsize) 不会停止请求输入,直到您收到文件结尾或错误。但是你知道需要多少输入,所以在 for 循环中编写代码。

【讨论】:

  • 但是为什么会这样呢?我也怀疑这一点,但根据 stackoverflow.com/questions/19483126/… ,如果 cin 的缓冲区变空,它应该终止还是我弄错了?
  • @noobnomore 你搞错了,cin &gt;&gt; x 如果它的缓冲区是空的,它将暂停,但它不会返回,直到它读取 x 或者它知道它可以' t(因为文件结束,i/o错误或格式错误)
  • 但答案是“终止”,我还在 Andrew Koening “Accelerated C++”中读到了他使用的内容:while(cin >> x){ ++count; sum += x;} 在 3.1 中,它起作用了。这就是为什么我很难回答。很抱歉打扰您,但您能详细说明一下吗?
  • @noobnomore 如果您执行while(cin &gt;&gt; x),那么 cin 将读取整个文件,因为 cin 只会在到达文件末尾时返回 false。但是您想读取指定数量的项目(但有多少在该行的其余部分),因此读取整个文件是不正确的。
  • @noobnomore 如果您不想在行首读取计数,另一种方法是使用 std::getline 一次读取一行,然后坚持在 std::stringstream 中并从那里读取各个项目。但这比仅在行首使用计数要多得多。
【解决方案2】:

我的回答。

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n,q;
    cin >> n >> q ;

    vector<int> a[n];

    int k;
    for(int i = 0; i < n; i++)
    {
        cin >> k; 
        for (int j = 0; j < k; j++)
        {
            int val_a_i_j;
            cin >> val_a_i_j;
            a[i].push_back(val_a_i_j);
        }
    };

    for (int i = 0; i < q; i++)
    {
        int a_i, j;
        cin >> a_i >> j;
        cout << a[a_i][j] << '\n';
    }

    return 0;
}

【讨论】:

    【解决方案3】:
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int main() {
        int a , b;
        cin >> a >> b;
        vector<int> arr[a];
        for(int i= 0 ; i < a ; i++)
        {
           int m;
           cin >> m;
           int o;
           for(int j=0;j<m;j++)
           {
              cin >> o;
              arr[i].push_back(o);
           }
        }
        int r,s;
        for(int k=1;k<b;k++)
        {
          cin>>r>>s;
          cout <<a[r][s]<< endl;
        }
        return 0;
    }
    

    【讨论】:

    • 嗨,欢迎来到堆栈溢出。要获得有用的答案,请解释为什么这是问题的答案。
    【解决方案4】:
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() 
    {
        int n, q;
        cin >> n >> q;
        vector<int>* a;
        a= new vector<int>[n];
        int p;
        int k;
        for (p = 0;p < n;p++) {
            cin >> k;
            for (int i = 0; i < k; i++) {
                int o;
                cin >>o;
                a[p].push_back(o);
            }
        }
        int r, s;
        for (p = 0;p < q;p++)
        {
            cin >> r >> s;
            cout << a[r][s]<<endl;
        }
        return 0;
    
    }
    

    【讨论】:

    • 嗨,请解释一下为什么这是问题的答案。
    • 虽然此代码可能会为问题提供解决方案,但最好添加有关其工作原理/方式的上下文。这可以帮助未来的用户学习并将这些知识应用到他们自己的代码中。在解释代码时,您也可能会以赞成票的形式从用户那里获得积极的反馈。
    【解决方案5】:

    这个问题是关于处理向量的向量。

    int main() {
      
        int n,q;
        cin>>n>>q;
        vector<vector<int> > a;//creating vectors of vector
        int k;
    
        for (int i = 0; i < n; i++)
        {
            cin >>k;
            vector <int> row; // creating vector
    
            for (int j = 0; j < k; j++)
            {
                int val;
                cin>> val;
                row.push_back(val);
            }
            a.push_back(row);
        }
        
        for (int l=0; l < q; l++)
        {
           int i,j;
           cin>>i>>j;
           // checking boundary conditions
           if ((i >= 0 && i < a.size() ) && (j >=0 && j < a[i].size()))
           {
              cout<<a[i][j]<<endl;
           }
          
        } 
        return 0;
    }
    

    【讨论】:

      【解决方案6】:
      #include <cmath>
      #include <cstdio>
      #include <vector>
      #include <iostream>
      #include <algorithm>
      using namespace std;
      
      int main() 
      {   
          vector<vector<int>> MultiArry;
          vector<int> output;
          int n1,n2, q;
          int i,j;
          
          cin >> n1 >> q;
          MultiArry.resize(n1);
          output.resize(q);
          for(int i=0;i<n1;i++)
          {
              cin >> n2; 
              MultiArry[i].resize(n2);
              for(int j=0;j<n2;j++)
              {
                  cin >> MultiArry[i][j];
              }
          }
          
          for(int ii=0;ii<q;ii++)
          {
              cin >> i >> j;
              output[ii] = MultiArry[i][j];
          }
          
          for(int i=0;i<q;i++)
              cout << output[i] << endl;
              
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-28
        • 1970-01-01
        • 2017-03-28
        • 2021-09-18
        • 2019-12-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多