表示带有输入的数组中的元素数?

Denote number of elements in an array with input?

本文关键字:元素 数组 输入 表示      更新时间:2023-10-16

这是输入:

输入文件的第一行包含一个整数 T,表示测试用例的数量。对于每个测试用例,将有两行。第一行包含 N,表示数组中的元素数,第二行包含 N 个空格分隔的整数。

我知道如何表示测试用例的数量,但不知道如何表示数组中的元素数量。 我对 c++ 很陌生,所以如果你回答菜鸟友好会很好。

它应该是这样的:

cin>>test;                                // Taking the number of test cases 
while(test--){                        // For each test case
cin>>n;                           // Taking input n
vector<int> a(n);
for(i=0;i<n;i++){
cin>>a[i];                   // Taking input as n integers
}
}
OR
cin>>test;                                     // Taking the number of test cases  
for(int i=0;i<test;i++){                   // For each test case
cin>>n;                                // Taking input n
vector<int> v;                         // declaring a vector
for(int j=0;j<n;j++){
cin>>x;
v.push_back(x);                    // Taking input as n integers one by one
}
}

要表示数组中元素的数量,您只需

int arr[N];

其中 N 表示数组中元素的数量。如果你使用的是 std::vector,你可以像这样初始化它:

vector <int> vec(N);

然后,您可以使用 for 循环将值输入到数组中。