题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1008
这个题目的难点在于如何理解Sample Input里的两个数17和41
起初我没理解题目真正的意思,以为每到一层都会停一下,所以第一个输入1 2 我理解成0->1 +6 停一下+5 1->2 +6 ,这样一算刚好是17。
但是根据这样的思路再来算3 2 3 1,结果是46了,我在Discuss里面看到也有人跟我一样的想法,怎么算都是46,于是就卡壳了,无法继续。
后来在Discuss里面看到一个提示,电梯只在输入的那几个目标层停,中间层不停。到达最后一层后也会停5秒。这样再一算,完全吻合了。0->2: 2*6+5, 2->3: 1*6+5, 3->1: 2*4+5 = 41
5468946 | 2012-03-04 21:01:39 | Accepted | 1008 | 0MS | 284K | 520 B | C++ | ajioy |
#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;
}
}
网友精简版:
#include <stdio.h>
int main()
{
int n,a,c,s;
while(~scanf("%d",&n),n) {
s = c = 0;
while(n--) {
scanf("%d",&a);
s += (a > c? (a - c) * 6 + 5: (c - a) * 4 + 5);
c = a;
}
printf("%d\n",s);
}
}