`
richard_ma
  • 浏览: 15151 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

hud1008 电梯 迭代模拟计算

阅读更多
Elevator

http://acm.hdu.edu.cn/showproblem.php?pid=1008

Problem Description
The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input
There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.

Output
Print the total time on a single line for each test case.

Sample Input
1 2
3 2 3 1
0

Sample Output
17
41

解题思路
完全的模拟题目叙述过程。
当last-t>0时说明电梯下楼,速度按照4s计算;否则电梯上楼,按照6s计算。
电梯每次到达指定楼层停止的时间刚好是n*5。

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char const* argv[])
{
    int n, ans, last, distance, i, t; 

    while (scanf("%d", &n) != EOF) {
        if (0 == n) break;

        ans = n * 5;
        last = 0;
        for (i = 0; i < n; i++) {
            scanf("%d", &t);
            distance = last - t;

            if (distance > 0) {
                ans += distance * 4;
            } else {
                ans += abs(distance) * 6;
            }

            last = t;
        }

        printf("%d\n", ans);
    }

    return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics