欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

elevator

程序员文章站 2022-03-21 11:45:55
...

问题: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);	
        }
        
	}

}