• ACM Elevator


    Elevator

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 101667 Accepted Submission(s): 55108

    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
    

    分析:题意不难,简单题,就是按顺序到指定楼层,并且无论中间还是最后结束,都要加5(stay for 5 seconds at each stop)这个分析example得出

    写代码的时候发现:每到一层,都要把现在的状态改变

    #include<iostream>
    using namespace std;
    int main()
    {
    	int n;
    	int sum;
    	while(cin>>n&&n!=0)
    	{
    		int a = 0;
    		sum = 0;  //注意sum清0
    		while(n--)
    		{
    			int b;
    			cin>>b;
    			if(a>b)
    			{
    				sum+=4*(a-b);
    			}
    			else
    			{
    				sum+=6*(b-a);
    			}
    			sum+=5;
    			a = b;      //改变状态,现在在b楼
    		}
    		cout<<sum<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    iframe之间操作记录
    Windows平台下nginx跨域配置
    git 常用命令
    单引号和双引号
    Mybatis Generator配置详解
    IOS仿桌面拖动桌面图标
    Bash Shell基础笔记
    mysql服务启动异常:windows无法启动Mysql服务,位于本地计算机上的错误1053 解决
    lombok笔记----Lombok常用注解
    thrift笔记----大体上thrift知识
  • 原文地址:https://www.cnblogs.com/serendipity-my/p/12601421.html
Copyright © 2020-2023  润新知