PAT甲级1008. Elevator

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 Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.

Output Specification:

For each test case, print the total time on a single line.

Sample Input:
3 2 3 1

Sample Output:
41

题目要求:

一部电梯,给N个正整数,表示电梯将依次在哪一层楼停止,上升一层楼需要6S,下降一层楼需要4S,电梯在每一层楼停止5S;

给予的每一列数需要计算电梯停过每一层楼索要要的总的时间,开始时电梯在0层,且结束后电梯不要要回到0层;

每一个输入包括,一个正整数N,在这个正整数之后是N个正整数(这个是重点!!!!坑爹的题目给了输入的例子:3X6+4X1+6X1+4X2+5=41!!!2X6+1x6+2X4+3x5=41!!!)本宝宝才不会告诉你:我的神奇脑回路;所有的输入数不超过100个;

解题思路:

首先将输入的数字存储在一个数组中(题目给出的输入不超过100,所以可以定义长度为100的数组),然后循环比较数组的每一个元素与前后元素的大小,确定是上升还是下降;并计算时间;

#include "stdlib.h"
#include "stdio.h"


int main() {

    char ch;
    int floor[101];
    //用于对输入的楼层数进行计数
    int count = 0;
    int time =0 ;
    scanf("%d", &floor[count++]);
    while (ch=getchar()!='\n')
    {
        scanf("%d", &floor[count++]);
    }
    time += 6 * floor[1]+5;

    for (int i = 2; i < count; i++)
    {
        if (floor[i]>floor[i-1])
        {
            time += 6 * (floor[i] - floor[i - 1])+5;
        }
        else
        {
            time += 4 * (floor[i-1] - floor[i])+5;
        }
    }

    printf("%d",time);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值