PAT Advanced Level 1008 Elevator
1. 问题描述

2. Solution
1、思路分析
题目大意:电梯从0层开始向上,给出该电梯依次按顺序停的楼层数,并且已知上升需要6秒/层,下降需要4秒/层,停下来的话需要停5秒,问走完所有需要停的楼层后总共花了多少时间~
分析:累加计算输出~now表示现在的层数,a表示将要去的层数,当a > now,电梯上升,需要6 * (a – now)秒,当a < now,电梯下降,需要4 * (now – a)秒,每一次需要停5秒,最后输出累加的结果sum~
2、代码实现
// PAT Advance Level 1008 // Ye Qiu #include <iostream> #include <cstdio> using namespace std; int main() { #ifdef ONLINE_JUDGE #else freopen(input/1008.txt, r, stdin); #endif int up = 6, down = 4, stop = 5, res = 0, cur = 0, goal; cin >> goal; while (cin >> goal) { if (goal > cur) { // up res += (goal - cur) * up; } else { res += (cur - goal) * down; } res += stop; cur = goal; } printf(%d, res); return 0; } 3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(1)