杭电acm1008 hdu-acm-1008解题报告

来源:互联网 发布:java类声明 编辑:程序博客网 时间:2024/05/30 23:49

题目地址:

点此打开题目地址

题目:

Elevator

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 43034    Accepted Submission(s): 23608


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 23 2 3 10
 

Sample Output
1741


        大意和思路:这个题的思路比较简单,当上一层楼的时候,每上一层楼用6个小时,电梯在楼层停留5个小时,每下一层楼电梯用4个小时;计上下楼的顺序是啊【n】;n<100;如果是上楼,时间是(a[i]-a[i-1])*6+5;下楼是(a[i]-a[i-1])*4+5;






代码:
#include<stdio.h>int t(int a,int b){return a>b?(a-b)*4+5:(b-a)*6+5;}main(){int i,j,k,m,n,T,a[110],time;while(scanf("%d",&T)&&T){for(i=0;i<T;i++)scanf("%d",&a[i]);time=0;time+=t(0,a[0]);for(i=1;i<T;i++)time+=t(a[i-1],a[i]);printf("%d\n",time);}}

0 0
原创粉丝点击