<div class="panel_title" align="left" style="font-family: 'Microsoft Yahei', 'Microsoft Yahei'; height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; text-shadow: rgb(153, 153, 153) 0.01em 0.01em 0.01em !important; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">Problem Description</div><div class="panel_content" style="font-family: 'Microsoft Yahei', 'Microsoft Yahei'; height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; text-shadow: rgb(153, 153, 153) 0.01em 0.01em 0.01em !important; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;">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.<br style="text-shadow: rgb(153, 153, 153) 0.01em 0.01em 0.01em !important;" /><br style="text-shadow: rgb(153, 153, 153) 0.01em 0.01em 0.01em !important;" />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.<br style="text-shadow: rgb(153, 153, 153) 0.01em 0.01em 0.01em !important;" /></div><div class="panel_bottom" style="font-family: 'Microsoft Yahei', 'Microsoft Yahei'; height: auto; margin: 0px;font-size:14px; text-align: -webkit-center; text-shadow: rgb(153, 153, 153) 0.01em 0.01em 0.01em !important; background: url(http://acm.hdu.edu.cn/images/panel-bottom.png) 0% 0% no-repeat;"> </div><br style="font-family: 'Microsoft Yahei', 'Microsoft Yahei';font-size:14px; text-align: -webkit-center; text-shadow: rgb(153, 153, 153) 0.01em 0.01em 0.01em !important;" /><div class="panel_title" align="left" style="font-family: 'Microsoft Yahei', 'Microsoft Yahei'; height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; text-shadow: rgb(153, 153, 153) 0.01em 0.01em 0.01em !important; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">Input</div><div class="panel_content" style="font-family: 'Microsoft Yahei', 'Microsoft Yahei'; height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; text-shadow: rgb(153, 153, 153) 0.01em 0.01em 0.01em !important; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;">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.<br style="text-shadow: rgb(153, 153, 153) 0.01em 0.01em 0.01em !important;" /></div><div class="panel_bottom" style="font-family: 'Microsoft Yahei', 'Microsoft Yahei'; height: auto; margin: 0px;font-size:14px; text-align: -webkit-center; text-shadow: rgb(153, 153, 153) 0.01em 0.01em 0.01em !important; background: url(http://acm.hdu.edu.cn/images/panel-bottom.png) 0% 0% no-repeat;"> </div><br style="font-family: 'Microsoft Yahei', 'Microsoft Yahei';font-size:14px; text-align: -webkit-center; text-shadow: rgb(153, 153, 153) 0.01em 0.01em 0.01em !important;" /><div class="panel_title" align="left" style="font-family: 'Microsoft Yahei', 'Microsoft Yahei'; height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; text-shadow: rgb(153, 153, 153) 0.01em 0.01em 0.01em !important; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">Output</div><div class="panel_content" style="font-family: 'Microsoft Yahei', 'Microsoft Yahei'; height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; text-shadow: rgb(153, 153, 153) 0.01em 0.01em 0.01em !important; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;">Print the total time on a single line for each test case. </div>
#include<iostream>
#include<cstring>
using namespace std;
int main(void)
{
int temp,i,n,sum=0;
while(cin>>n && n!=0){
int count=0; //不定义为0直接比较的话,会乱码
for(i=0;i<n;i++) {
cin>>temp; //用count来记录之前的楼层
if(temp>count) sum+=(temp-count)*6;
else if(temp<count) sum+=(count-temp)*4;
sum+=5;
count=temp;
}
cout<<sum<<endl;sum=0;
}
}
无它,属于OJ上最基本的一类题,只要读懂题意,计算好上升时间、停留时间、下降时间,就能写出代码来。