计算多组数据中电梯的不同路径所用的时间

来源:互联网 发布:mac os下载 编辑:程序博客网 时间:2024/06/06 18:18

Problem DescriptionThe 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.InputThere 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.OutputPrint the total time on a single line for each test case. Sample Input1 23 2 3 10Sample Output1741#include <iostream>using namespace std;int main(){   int a[100],n,i;   while(cin>>n&&n!=0)   {       int s=0;       for(i=0;i<n;i++)        cin>>a[i];        s+=a[0]*6+5;        for(i=1;i<n;i++)        {            if(a[i]>a[i-1])                s+=6*(a[i]-a[i-1])+5;            else                s+=4*(a[i-1]-a[i])+5;        }        cout<<s<<endl;   }    return 0;}
<img src="http://img.blog.csdn.net/20150127134140001?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYTcxNjEyMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


0 0