我擦,C/C++ 里支持 int a[n]; 这种写法了???

2013-11-15 17:44:50 +08:00
 xieranmaya
记得我上大学的时候这么个写法是不行的啊
C里要用malloc
C++里要用new
为毛现在可以这么写啊?是新标准的缘故?
大家可以拿这个代码在SOJ.ME上去测http://soj.me/1134这个题目
看while下面的两行代码

#include <stdio.h>
#include <stdlib.h>

void main() {
int n,s;
int i = 0;
int j = 1;
int t = 1;
int flag = 1;

while(scanf("%d%d",&n,&s) && n != 0){
int a[n];
int b[n];
for(i=0; i<n; i++){
scanf("%d%d", &a[i], &b[i]);
}

for(i=0; i<n; i++){
for(j=i+1; j<n; j++){
if(b[i] >= b[j]){
t = b[i];
b[i] = b[j];
b[j] = t;
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}

for(j=0; j<n; j++){
if(s < b[j]){
flag = 0;
break;
}
s = s+a[j];
}
if(flag == 0){
printf("NO\n");
}else{
printf("YES\n");
}
}
exit(0);
}
4612 次点击
所在节点    程序员
21 条回复
chisj
2013-11-18 14:52:34 +08:00
@yegle 不小心可能导致栈溢出啊,txx9楼说了.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/89431

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX