简体   繁体   English

可变大小多维数组

[英]Variable size multidimensional array

What is problem with my code for variable size multidimensional array.How to fix this problem.我的可变大小多维数组的代码有什么问题。如何解决这个问题。 My code is not passing all test cases.Can anyone help me to fix it.This is question from hackerrank challenge.我的代码没有通过所有测试用例。谁能帮我修复它。这是来自hackerrank挑战的问题。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n;
    cin>>q;
    int *arr[n];  
    for(int i=0;i<n;i++)
    {
       int x;
       cin>>x;
       int b[x];
       for(int j=0;j<x;j++)
       {
           cin>>b[j];
       } 
       arr[i]=b;
    }
    while(q--)
    {
        int i,e;
        cin>>i>>e;
        cout<<arr[i][e]<<endl;
    }
    return 0;
}

Here is correct code.这是正确的代码。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n;
    cin>>q;
    int *arr[n];   // passed all test cases
    for(int i=0;i<n;i++)
    {
       int x;
       cin>>x;
       int *b=new int[x];
       for(int j=0;j<x;j++)
       {
           cin>>b[j];
       } 
       arr[i]=b;
    }
    while(q--)
    {
        int i,e;
        cin>>i>>e;
        cout<<arr[i][e]<<endl;
    }
    return 0;
}

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

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