pat1008
来源:互联网 发布:mysql innodb表修复 编辑:程序博客网 时间:2024/05/19 13:15
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 4using 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" 之间的空格 。
- PAT1008
- PAT1008
- pat1008
- PAT1008
- pat1008
- PAT1008
- PAT1008
- pat1008
- pat1008
- PAT1008. Elevator
- PAT1008. Elevator
- pat1008 Elevator
- PAT1008总结
- PAT1008. Elevator (20)
- pat1008段错误
- PAT1008. Elevator (20)
- pat1008. Elevator (20)
- PAT1008. Elevator (20)
- 基于jQuery的TreeGrid组件详解
- Java 新特性入门
- 程序员你值得拥有的常用软件
- Game of Connections 卡特兰数
- 【腾讯开源】MT:专注于移动端的JavaScript模块管理框架
- pat1008
- (单调栈)Bad Hair Day
- 关于typedef的用法总结
- 多重继承、派生类
- C# WebService 参数属性
- DevOps=Dev+Ops
- 通向架构师的道路(第七天)之漫谈使用ThreadLocal改进你的层次的划分
- JDK8 部分代码测试
- 项目经验:Query 日期参数的设置方法setTimestamp