輸入多行,每行首數 n 為停靠樓層數,隨後 n 個樓層號。電梯從 0 樓開始:上一層 6 秒,下一層 4 秒,停靠 5 秒。電梯無需返回 0 樓。求總耗時。
curr(初始 0)。stop[i]:stop[i] > curr:time += (stop[i]-curr)*6stop[i] < curr:time += (curr-stop[i])*4curr = stop[i]。#include#define MAXN 100 int main() { int n, stop[MAXN+1], i; int curr, total; while (scanf("%d", &n) != EOF) { if (n == 0) break; for (i=0; i curr) total += (stop[i] - curr) * 6; else total += (curr - stop[i]) * 4; total += 5; curr = stop[i]; } printf("%d\n", total); } return 0; }
基礎模擬題,注意輸入結束條件與上下樓時間差異。