PAT 1008 Elevator

PAT甲级1008题Elevator (20 分)题解。

原题链接

题目:   

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 1

Sample Output:  

41

翻译:

电梯

我们城市最高的建筑只有一部电梯。 请求列表由N个正数组成。 数字表示电梯将以指定顺序停在哪些楼层。 将电梯向上移动一层需要6秒钟,向下移动一层楼需要4秒钟。 电梯将在每个站点停留5秒钟。

对于给定的请求列表,您将计算在列表上完成请求所花费的总时间。 电梯在开始时位于0楼,并且在满足要求时不必返回底层。

输入规格:

每个输入文件包含一个测试用例。 每个案例包含一个正整数N,后跟N个正数。 输入中的所有数字都小于100。

输出规格:

对于每个测试用例,请在一行上打印总时间。

样本输入:

3 2 3 1

样本输出:

41

不多说,一道模拟题,很水,就不做题目分析了,直接上代码。

#include
int main(){
	int num;     //一共去num个楼层
	int floor = 0;     //起始楼层为0
	scanf("%d",&num);   
	int totaltime = 0;    //设置总时间
	while(num--){       
		int dinfloor;    //当前循环的目的地址  
		scanf("%d", &dinfloor);    
		int movefloor = dinfloor - floor;   //设置需要移动的楼层
		if(movefloor > 0){        //大于零为向上
			totaltime = 6 * movefloor + totaltime + 5;
		}else{                       //小于零为向下
			totaltime = (-4) * movefloor + totaltime + 5;
		}
		floor = dinfloor;     //设置目前楼层位置,为下次移动做准备。
	} 
	printf("%d",totaltime);
} 

 

 

你可能感兴趣的:(PAT)