zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    C# 中的本地函数
    C# 9.0 正式发布了(C# 9.0 on the record)
    如何禁用控制台窗口的关闭按钮?
    在 WSL Ubuntu 上使用 .NET 进行跨平台开发新手入门
    C# 中 ConcurrentDictionary 一定线程安全吗?
    Docker 与 Podman 容器管理的比较
    C# 中的数字分隔符 _
    C# 8: 可变结构体中的只读实例成员
    C# 中的只读结构体(readonly struct)
    C# 8: 默认接口方法
  • 原文地址:https://www.cnblogs.com/serendipity-my/p/12601421.html
Copyright © 2011-2022 走看看