C和GCC的一些区别
题目是:elevator.
题目描述:
The highest building in our city has only one elevator. A
request list is made up with N positive numbers. The numbers denote
at which floors the elevator will stop, in specified order. It
costs 6 seconds to move the elevator up one floor, and 4 seconds to
move down one floor. The elevator will stay for 5 seconds at each stop.
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.(英语水平有点渣,勉强看得懂,翻译啥的我还是不丢人。。。)
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.(英语水平有点渣,勉强看得懂,翻译啥的我还是不丢人。。。)
代码如下:
#include "stdio.h"
int main(){
int a,time=0;
while(scanf("%d",&a)!=EOF&&a!=0){
int b[a+1];
b[0]=a;
while(a){
scanf("%d",&b[a]);
a--;
}
a=b[0];
time=b[a]*6+5;
for(;a>1;a--){
if(b[a]<b[a-1]){
time=time+(b[a-1]-b[a])*6+5;
}
else{
time=time+(b[a]-b[a-1])*4+5;
}
}
printf("%d\n",time);
}
return 0;
}
这段代码在OJ上选择C编译 出现compilation error错误
error C2057: 应输入常量表达式
error C2466: 不能分配常量大小为 0 的数组
error C2133: “b”: 未知的
大小
选择GCC 编译成功
看错误信息应该是数组那里两者的规范不同吧,如果用C的话应该怎么改好? 另外我这段代码还有可以改进的地方吗?希望大神可以不吝赐教