elevator

本文介绍了一个简单的算法,用于计算电梯完成一系列楼层请求所需的总时间。考虑到电梯上下移动所需的不同时间和在每层停留的时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题: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.

大概翻译:电梯从0层出发到达特定的层数,向上一层需要六秒,向下一层需要4秒,停一次需要5秒,当输入具体停的层数时,求总共的时间。

解决:输入具体的层数,遍历,计算

输入具体的层数:java.util.Scanner,循环遍历(用while或者for循环都行),计算,初始楼层f为0,到达第一个楼层floor的时间sum为:sum=(floor-f)*6+5。第二个楼层若向上,则sum=sum+(floor-f)*6+5,若向下,则sum=sum+(f-floor)*4+5;

循环:

循环分三种:for语句,while语句,do...while语句

while语句:

[初始化部分]

while(循环条件){

循环体,包括迭代部分

}

代码还有点问题,应该是循环的问题

 

package sequence;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class elevator {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int f,sum;//分别代表到达的楼层,和时间总数
        
//        System.out.println("数字"+n);
        while(scan.hasNext()){
        	if(n==0){
        		sum=0;
        		break;
        	}else{
        		    f=0;//上一层数
   			        sum=0;
        		while(n-->0){
        		
        			if(n>f){
        				sum = sum+(n-f)*6+5;
        				f = n;
        			}else{
        				sum = sum+(n-f)*4+5;
        				f = n;
        			}
        		}
        	}
        	System.out.println(sum);	
        }
        
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值