• 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;
    }
    
  • 相关阅读:
    用jQuery的offset()替代javascript的offset
    java实现SAP BO登录
    原生js实现模块来回拖拽效果
    HTML中marquee标签的使用
    axure 预览"HTTP/1.1 302 Found"
    redis命令详解
    idea找不到package下的mapper.xml文件
    confluence搭建详情
    httpClient解决post请求重定向的问题
    java double相加
  • 原文地址:https://www.cnblogs.com/serendipity-my/p/12601421.html
Copyright © 2020-2023  润新知