HDU 1008电梯问题

Elevator

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

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


思路

1、题目比较水,秒过。大概题意:就是说现在有个升降机,上升一层就要花6秒时间,到了目标楼层后会停留5秒时间,下降一层要花4秒时间,你一开始现在在0层,OJ提供你楼层信息,你计算出整个过程所花费的时间。
2、注意一点就是每行第一个数据代表之后有几组数据。并非算做楼层的

C++解法(OJ C++)

#include <iostream>
using namespace std;

int main()
{
    int a[101]={0};
    while(cin>>a[0])
    {
        int end=0;
        int begin=0;
        int total=0;
        int count=1;
        if(a[0]==0)
            break;
        for(int i=1;i<=a[0];i++)
        {
            cin>>a[i];
            count++;
        }
        for(int i=1;i<count;)
        {
            end=a[i];
            if(end-begin>0)
            {
                total+=((end-begin)*6+5);
                i++;
                begin=a[i-1];
            }
           else
            {
                total+=((begin-end)*4+5);
                i++;
                begin=a[i-1];
            }
        }
        cout<<total<<endl;
    }
    return 0;
}
HDOJ 1443题目通常指的是“Hello, World!” in Hanoi problem,这是一个经典的递归问题,也称为汉诺塔(Hanoi Tower)问题。这个题目要求你将一个盘子从起始柱子移动到目标柱子,但每次只允许移动一个盘子,并且大盘子不能放在小盘子之上。 具体描述是这样的:有三个柱子A、B、C,以及若干个大小不同的圆盘,初始时所有圆盘都放在柱子A上,按照圆盘大小从小到大依次排列。目标是从A柱子将所有盘子移动到C柱子,过程中每一步只能移动一个盘子,并且任何时候都不能让大盘子放在小盘子之上。 解法通常是使用递归来描述步骤: 1. 将最上面的n-1个盘子从A移动到B。 2. 将最大的盘子从A移动到C。 3. 最后,将之前在B上的n-1个盘子从B移动到C。 这里的关键在于分治策略,将大问题分解成几个相似的小问题进行处理。在Java中,你可以创建一个方法接受三个参数:源柱子、目标柱子和剩余要移动的盘子数。以下是一个简单的递归解决方案: ```java public void hanoi(int n, char from, char to, char aux) { if (n == 1) { System.out.println("Move disk 1 from " + from + " to " + to); } else { hanoi(n - 1, from, aux, to); // 移动n-1个到辅助位置 System.out.println("Move disk " + n + " from " + from + " to " + to); // 移动最大的到目标位置 hanoi(n - 1, aux, to, from); // 再次移动n-1个到目标位置 } } ``` 要运行此程序并输出解决方案,只需调用`hanoi(n, 'A', 'C', 'B')`,其中'n'是圆盘的数量。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值