hdu-oj 1008 Elevator

来源:互联网 发布:sql美化工具 编辑:程序博客网 时间:2024/06/05 20:13

Elevator

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


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
 
题目大意:输入N表示要上的楼层,从第一层起始。计算所需时间;
解题思路:上一层花费6s停留5s,下楼4s。
附代码:
#include <stdio.h>#include <string.h>int main(){  int n,m,i,j;  int a[110],sum;  while(scanf("%d",&n)!=EOF&&n!=0)  {  memset(a,0,sizeof(a));sum=0;     for(i=1;i<=n;i++)       scanf("%d",&a[i]);       a[0]=1;     for(i=0,j=i+1;i<n;j++,i++)      {  if(a[i]<a[j])           sum+=(a[j]-a[i])*6;         else            sum+=(a[i]-a[j])*4;                  }      printf("%d\n",sum+(n+1)*5+1);  }    return 0;}

 
0 0
原创粉丝点击