1008. Elevator (20)
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 Specification:
Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.
Output Specification:
For each test case, print the total time on a single line.
Sample Input:3 2 3 1Sample Output:
41
#include <cstdio>
#include <string.h>
#define Wait 5
#define Up 6
#define Down 4
using namespace std ;
int main ( void )
{
int num = 0 ,temp , total = 0 ;
int list[205] ;
memset ( list , 0 , sizeof(list)) ;
while(1)
{
scanf("%d" , &temp) ;
char c =getchar() ;
list[num++ ] = temp ;
if ( c == '\n')
break ;
}
num = list[0] ;
list[0] = 0 ;
total += num*5 ;
for ( int i = 0 ; i < num ; i++ )
{
if ( (list[i] - list[i+1]) < 0 )
{
total += Up * (list[i+1] - list[i] ) ;
}
else if ( (list[i] - list[i+1] > 0 ))
{
total += Down*(list[i] - list[i+1] ) ;
}
}
printf("%d", total ) ;
return 0 ;
}
这道题从总体上来讲需要思考和计算的东西并不是很多,不过对于新手(LZ)而言,容易忽略的地方时数据的接收格式,本题的数据接收格式与以往的全部是字符串的接收方式和先给出case 的个数然后再给出案例数据的接收方式不同。本题中的数据例子个数与数据所在的是一行,如果使用 scanf() 来接收,后面的空格符处理不得当的话,很容易造成读入数据错误。 如果使用字符串接收的方法,则需要对字符进行转换为int 类型,根据本题描述数据,数字的个数 <= 100 , 一旦接收的数据为 2 位或是3 位的整数,还需要对其进行累加使之成为整型数据,不过可以通过
while (1 )
{
scanf( list[num++]) ;
char c = getchar() ;
if ( c =='\n' )
break ;
}
的方式来对这种案例输入的数据进行接收,最终 num 的数目即为list[] 中存放入元素的个数,同时 char c 一共有两个作用,一个是用来判断循环是否结束,另一个是用来接收数据中例如 "1 2 3" 之间的空格 。