您的位置:首页 > 其它

Elevator解题报告

2013-07-28 21:23 162 查看
题目摘要:The highest building in our cityhas only one elevator. A request list is made up with N positive numbers. Thenumbers denote at which floors the elevator will stop,
in specified order. Itcosts 6 seconds to move the elevator up one floor, and 4 seconds to move downone 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 fulfillthe requests on the list. The elevator is on the 0th floor at the beginning anddoes not have to return to the ground floor when the requests are fulfilled.

题目大意:电梯问题,一架电梯位于第0层,上升一层用6秒,下降一层用4秒,停在一层用5秒,给出一串命令求所用时间。

输入输出要求

Input

There are multiple test cases. Each casecontains a positive integer N, followed by N positive numbers. All the numbersin the input are less than 100. A test case with N = 0 denotes the end ofinput. This test case is not to be
processed.

 

Output

Print the total time on a single line foreach test case.

 

输入输出样例

Sample Input

1 2

3 2 3 1

0

 

Sample Output

17

41

 

解题思路:直接开一个数组,比较第i项和前一项的大小,然后加上相应的时间。

代码

#include<iostream>

using namespace std;

int num[105];

int main()

{

    int N;

    num[0]=0;

    while(cin>>N)

    {

           if(N==0)

             
return0;

           else

           {

             
int i;

              int sum=0;

              for(i=1;i<=N;i++)

              {

                  cin>>num[i];

                  if(num[i]>num[i-1])

                      
sum=sum+6*(num[i]-num[i-1])+5;

                      
else if(num[i]==num[i-1])

                          
sum=sum+5;

                      
else if(num[i]<num[i-1])

                          
sum=sum+4*(num[i-1]-num[i])+5;

              }

              cout<<sum<<endl;

           }

    }

    return 0;

}

解题感想:水题一枚,不想多说。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  解题报告