在我的程序中哪里出错? [英] where exactly went wrong in my program??
问题描述
我正在尝试创建一个N维向量,每个向量都有i个元素,然后我需要将所有向量添加到一个向量中,但是,如果输入是:
*例如:
N = 2
i = 3
first vector = 1 2 3
$
秒vector = 4 5 6
答案应该是5 7 9
但我得到的是2 6 12.为什么??
int main(){
int N,i;
cin>> N;
cin>> i;
long long int元素;
vector< vector< long long int>> vec;
vector< long long int> vec1;
for(int x = 0; x< N; x ++)
{
for(int y = 0; y< i ; y ++)
{
cin>> element;
vec1.push_back(element);
}
vec.push_back(vec1);
}
long long int sum = 0;
vector< long long int> ; new_vec;
for(int h = 0; h< i; h ++)
{
for(int e = 0; e< N; e ++ )
{
sum + = vec [e] [h];
}
new_vec.push_back(sum);
}
< span style ="white-space:pre"> for(int z = 0; z< i; z ++)
{
cout<< new_vec [z]<< " " ;;
}
system(" pause");
}
2016年11月3日晚上11:48,为什么dot net优于non dot net写道:
vector< ; vector< long long int>> vec;
$
vector< long long int> vec1;
for(int x = 0; x< N; x ++)
{
for(int y = 0; y< ; i; y ++)
{
cin>>元素;
$
vec1.push_back(元素);
}
vec.push_back(vec1);
在这附近的某处清除vec1,或在循环内移动它的声明。如上所述,vec最终会持有两个向量{1,2,3}和{1,2,3,4,5,6},因为你只是不断向vec1添加元素。
< a name ="x_x_e5d12c85-e12e-46f0-b91e-4063288554e8_CommunityBridge"title ="Mozilla / 5.0(Windows NT 6.1; WOW64; rv:45.0)Gecko / 20100101Thunderbird / 45.4.0通过社区论坛NNTP服务器1.1.55.0"> 一>
I am trying to create an N dimensional vectors and each with i elements, then i need to add all the vectors into a single vector, However, if the inputs are:
* for example:
N=2
i=3
first vector=1 2 3
second vector=4 5 6
the answer should be 5 7 9
but what i got is 2 6 12. Why??
int main() {
int N,i;
cin >> N;
cin >> i;
long long int element;
vector<vector<long long int>> vec;
vector<long long int> vec1;
for (int x = 0; x < N; x++)
{
for (int y = 0; y < i; y++)
{
cin >> element;
vec1.push_back(element);
}
vec.push_back(vec1);
}
long long int sum = 0;
vector<long long int> new_vec;
for (int h = 0; h<i; h++)
{
for (int e = 0; e<N; e++)
{
sum+=vec[e][h];
}
new_vec.push_back(sum);
}
for (int z = 0; z < i; z++)
{
cout << new_vec[z] << " ";
}
system("pause");
}
On 11/3/2016 11:48 PM, Why is dot net better than non dot net wrote:
vector<vector<long long int>> vec;
vector<long long int> vec1;
for (int x = 0; x < N; x++)
{
for (int y = 0; y < i; y++)
{
cin >> element;
vec1.push_back(element);
}
vec.push_back(vec1);Clear vec1 somewhere around here, or move its declaration inside the loop. As written, vec ends up holding two vectors, {1, 2, 3} and {1, 2, 3, 4, 5, 6}, because you just keep adding elements to vec1.
这篇关于在我的程序中哪里出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!