pat1008

19 篇文章 0 订阅

1008. Elevator (20)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 1
Sample 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" 之间的空格 。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值