杭电1008 Elevator

以前做这题的时候,答案跟它的示例一样,却发现老是Wrong Answer。今天再做的时候,终于想明白了原因,就是没有考虑到电梯连续去同一楼的情况,比如:3 0 0 0, 答案应该是 3*5=15, 而不是 0 !

因此重做一遍后,果然AC了!

以下是代码:

/* THE PROGRAM IS MADE BY PYY */ /* http://acm.hdu.edu.cn/showproblem.php?pid=1008 Elevator */ /* Test data: */ #include <iostream> using namespace std; int main() { int test_case, i, curFloor, lastFloor, time, differ; const int up = 6, down = 4, stay = 5; while (cin >> test_case && test_case) { time = i = curFloor = lastFloor = 0; while (i++ < test_case) { cin >> curFloor; differ = curFloor - lastFloor; if (differ > 0) { time += differ * up + stay; } else if (differ < 0) { time += (-1) * differ * down + stay; } else { time += stay; } lastFloor = curFloor; } cout << time << endl; } return 0; }

你可能感兴趣的:(UP)