0
点赞
收藏
分享

微信扫一扫

hodj 1008 Elevator (模拟题)


个人写的代码不够简洁,而且在处理这种多循环的代码时,每次循环时变量没有重新赋值为0,造成了调试了好几次代码才通过,这是不应该的。在这次代码中,time和current都没有重新赋值为0,下回应该注意。还要网友在代码中对题目的中时间常量进行了赋值,这一点很好,要学习。

代码如下:

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <stdio.h>
using namespace std;

int main() {
int n;
int floor;
int current;
int time;

while (1) {
time = 0;
current=0;
cin >> n;
if (n == 0) {
break;
}
while (n--) {
cin >> floor;
if(floor > current){
time=time+5+(floor-current)*6;
} else{
time=time+5+(current-floor)*4;
}
current=floor;
}
cout << time << endl;
}
return 0;
}

网友代码如下:

#include <iostream>
using namespace std;
int main()
{
const int UP = 6;
const int DOWN = 4;
const int STOP = 5;
int nCase,floor;
while(cin >> nCase && nCase){
int sec = 0,tmp;
//第一个目标层是由第0层出发,较特殊,单独算
cin >> floor;
tmp = floor;
sec = floor * UP + STOP; //由0层出发到第一个目标层所有时间
for(int i = 1; i < nCase; ++i){
cin >> floor;
if(floor > tmp) //如果电梯往上
sec += (floor - tmp) * UP + STOP;
else //电梯往下
sec += (tmp - floor) * DOWN + STOP;
tmp = floor;//记录本次目标层,方便下一个目标层的计算
}
cout << sec << endl;
}
}

 

 

 

 

 

 

举报

相关推荐

0 条评论