Elevator
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 87063 Accepted Submission(s): 47582
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 2
3 2 3 1
0
Sample Output:17
41
Author ZHENG, Jianqiang
题目大意:有一个很高的楼,开始电梯处于零层,电梯运行的方式就是:上升一层需要的时间是6s,下降一层所需要的时间是4S,每层的停留时间是5S。给出电梯的运行楼层指令,输出运行时间。
分析样例输入输出:
第一组数据:1 2 表示1个数据 2
1、零层到2层12S 总计12S
2、停留5S 总计17S
第二组数据:3 2 3 1
1、零层到2层 需要时间12S 总计12S
2、停留5S 总计:17S
3、2层到3层 需要时间 6S 总计:23S
4、停留时间 5S 总计:28S
5、3层到1层 需要时间8S 总计36S
6、停留时间 5S 总计41S
第三组数据: 0
不做处理
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string.h>
using namespace std;
int n;//每个样例的指令数目
int main(){
while(~scanf("%d",&n)&&n){
int last = 0;//用来记录当前楼层
int temp;//用来记录将要前往楼层
int sum = 0;//用来记录总计的时间
for(int i=0;i<n;i++){
scanf("%d",&temp);
if(temp>last){//如果是上升的话
sum+=(temp-last)*6;
}
else{//如果是下降的话
sum+=(last-temp)*4;
}
//暂停时间
sum+=5;
last = temp;
}
//输出格式每个样例输出一行
printf("%d\n",sum);
}
return 0;
}
题解代码:
#include<stdio.h>
int n;
int main()
{
int x;
while(~scanf("%d",&n))
{
if(n==0) break;
int now=0,ans=0;//now表示当前电梯所在楼层
for(int i=1;i<=n;++i)
{
scanf("%d",&x);
if(x>now) ans+=(x-now)*6;
else if(x<now)ans+=(now-x)*4;
now=x;
}
printf("%d\n",ans+5*n);
}
return 0;
}