这个题目的难点在于如何理解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
现在将代码贴在下面(自己的):
#include <stdio.h> #include <stdlib.h> int main() { int n; int a,b; int time=0; while(~scanf("%d",&n),n) { b=0; while(n--) { scanf("%d",&a); if(a-b>=0) time+=6*abs(a-b)+5; else time+=4*abs(a-b)+5; b=a; } printf("%d\n",time); } }
但是主要问题是老显示答案有错误,不知道是为什么。,原来是时间的初始化错了。time应该初始化在里面,现在将通过的别人代码贴在下面:
#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); } }