唐磊的个人博客

pat1008—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

这个题目重要要理解题意。

include <stdio.h>
#include <stdlib.h>
int main()
{
int total=0,i=0,sum=0;
int * array;
scanf("%d",&total);
array =(int *)calloc(total,sizeof(int));
array[0]=0;//0th floor at the beginning;
for(i=1;i<=total;i++)
{
scanf("%d",&array[i]);
}
for(i=0;i<total;i++)
{
if(array[i+1]>array[i]){//up
sum += (array[i+1]-array[i])*6;
sum+=5;
}else{//down
sum += (array[i]-array[i+1])*4;
sum+=5;
}
}
printf("%d",sum);

return 0;
}
tanglei wechat
欢迎扫码加入互联网大厂内推群 & 技术交流群,一起学习、共同进步