【发布时间】:2020-07-23 09:57:12
【问题描述】:
以下是代码,我已经一一尝试了 cmets 中提到的所有其他样式,但没有一个可以删除垃圾值,虽然“jugaad”有效,但性能不佳。不知道怎么回事!
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,m;
cin>>n>>m;
// Other styles
// int data[n+1][m+1] = {0,0};
// int data[n+1][m+1] = {0};
// int data[n+1][m+1] = {{0,0}};
int data[n][m] = {0};
// Jugaad start:
for(int i=0;i<m;i++){
data[0][i] = 0;
}
// Jugaad end
cout<<"\n";
for(int x=0;x<n;x++){
for(int y=0;y<m;y++){
cout<<data[x][y]<<"\t";
}
cout<<"\n";
}
cout<<"\n";
return 0;
}
【问题讨论】:
-
不是答案,
int data[n][m]不是有效的 C++,数组大小必须在编译时知道。如果它适合你,那么你的编译器支持它作为扩展。您可能应该使用std::vector。