思路
- 读入总共的层数(确保层数不为0,否则直接退出循环)
- 层数为接下来循环的次数,每次读入楼层,注意每一次读入新的楼层数,意味着在该楼层都要停留5秒钟,即
time += 5;
为必要操作 - 接下来判断读入楼层与当前楼层之间的关系,分别对应上楼和下楼两种情况,
- 循环即将结束时,更新当前楼层。输出时间
- 即处理完一次。。。
#include <iostream>
using namespace std;
int main()
{
int n = 0; //总共层数
while(cin >> n && n!=0)
{
int now = 0; //当前层数
int time = 0; //当前轮数所用时间
for(int i=0; i<n; i++)
{
int temp;
cin >> temp;
time += 5; //每到新的一层,或者说每遇到一个新的数字都停5秒钟
if (temp > now)
{
time += (temp - now)*6; //上楼
}
else
{
time += (now - temp)*4; //下楼
}
now = temp; //更新当前楼层
}
cout << time << endl;
}
return 0;
}