首页 > 试题广场 >

Elevator (20)

[编程题]Elevator (20)
  • 热度指数:2076 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
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.

输入描述:
Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.


输出描述:
For each test case, print the total time on a single line.
示例1

输入

3 2 3 1

输出

41
推荐
本题是模拟类型
用fromFloor记录当前所在楼层,toFloor记录要达到的楼层。
如果toFloor < fromFloor,花费的时间为(fromFloor - toFloor)*4+5。
如果toFloor >= fromFloor,花费的时间为(toFloor - fromFloor)*6+5。

代码如下:
import java.io.PrintStream;
import java.util.Scanner;

public class Main {
	public static Scanner in = new Scanner(System.in);
	public static PrintStream out = System.out;

	public static void main(String[] args) {
		int n = in.nextInt();
		int time = 0;
		int toFloor,fromFloor = 0;
		
		for(int i=0;i<n;++i){
			toFloor = in.nextInt();
			time += moveElevator(fromFloor,toFloor);
			
			fromFloor = toFloor;
		}
		out.println(time);
	}

	// 从fromFloor到toFloor花费的时间
	private static int moveElevator(int fromFloor, int toFloor) {
		if(toFloor<fromFloor){
			return (fromFloor-toFloor)*4+5;
		}
		return (toFloor-fromFloor)*6 + 5;
	}
}

编辑于 2015-08-18 23:00:15 回复(0)
真的是水题啊。。
import java.util.Scanner;

/**
 * 1008. Elevator (20)
 * 
 * @author Jacob
 *
 */
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int res = 0, cur = 0;

		for (int i = 0; i < N; i++) {
			int floor = sc.nextInt();
			if (cur <= floor) {
				res += 6 * (floor - cur);
			} else {
				res += 4 * (cur - floor);
			}
			cur = floor;
		}
		res += 5 * N;
		System.out.println(res);
		sc.close();
	}
} 


发表于 2017-08-27 10:01:11 回复(0)
最喜欢水题了,oh,yeah~~~~
#include<stdio.h>

#include<iostream>

#include<string>

using namespace std;

int main()

{

    int N = 0,sum=0;

    scanf("%d",&N);

    int input[10000];

    input[0]=0;

    for(int i=1;i<=N;i++)

    {

        scanf("%d",&input[i]);

    }


    for(int i=1;i<=N;i++)

    {

        if(input[i]>input[i-1]) 

        {

            sum+=(input[i]-input[i-1])*6;

        }else if(input[i]<input[i-1]){

            sum+=(input[i-1]-input[i])*4;

        }

    }


    sum+=N*5;

    cout<<sum<<endl;

    return 0;

}

发表于 2018-02-15 22:41:36 回复(0)
#include<stdio.h>
int main (){//the shorter,the better.
    int n,i,t,p,q[1000];
    for(;~scanf("%d",&n);printf("%d\n",t+5*n)){
       for (i = 0; i < n&&~scanf("%d",&q[i]);i++);
       for (p=t=i=0;i<n;p==q[i]?:(t+=p<q[i]?6*(q[i]-p):4*(p-q[i])),p=q[i++]);
    }
}

发表于 2018-02-02 10:36:45 回复(0)
import java.util.Scanner;
//甲级练习题中最水没有之一 )=。=(
//另外一楼被推荐的java答案也太冗长了吧
public class Main {

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int a[]=new int[n+1];
		int time=0;
		a[0]=0;
		for(int i=1;i<n+1;i++){
			a[i]=in.nextInt();
			int cha=a[i]-a[i-1];
			time+=cha>0?cha*6:-cha*4;
			time+=5;
		}System.out.println(time);
	}

}


编辑于 2016-10-28 18:14:20 回复(0)
#include<bits/stdc++.h>
using namespace std;

int main() {
	int n,total=0;
	cin>>n;
	int a[1010];
	a[0]=0;
	for(int i=1; i<=n; i++) {
		cin>>a[i];
	}
	for(int i=1; i<=n; i++) {
		a[i]>a[i-1]?total+=(a[i]-a[i-1])*6+5:total+=(a[i-1]-a[i])*4+5;
	}
	cout<<total<<endl;
	return 0;
}

发表于 2022-11-14 15:54:42 回复(0)
#include<iostream>
using namespace std;
int main()
{
    int n,sum=0,now=0,next=0;
    cin>>n;
    while(n--)
    {
        cin>>next;
        int time=next>now?(next-now)*6+5:(now-next)*4+5;
        sum+=time;
        now=next;
    }
    cout<<sum<<endl;
    return 0;
}

发表于 2020-03-26 11:38:14 回复(0)
package PAT;

import java.util.Scanner;
//不小心将5秒看做4秒了,结果一直出错,做这道题差点怀疑人生了,
public class Elevator {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int from=0;
		int to=0;
		int sum=0;
		for(int i=0;i<n;i++) {
			to=sc.nextInt();
			if(from<to) {
				sum+=(to-from)*6+5;
				from=to;
			}else {
				sum+=(from-to)*4+5;
				from=to;
			}
		}
		System.out.println(sum);
	}
}

发表于 2019-08-13 16:40:25 回复(0)
水题
#include <iostream>
using namespace std;
int main()
{     int N, sum=0;     cin>>N;     int *a = new int [N+1];     a[0] = 0;     for (int i=1; i<=N; i++) {         cin>>a[i];         if (a[i]>a[i-1]) {             sum += (a[i]-a[i-1])*6;         } else if (a[i]<a[i-1]) {             sum += (a[i-1]-a[i])*4;         }         sum += 5;     }          cout <<sum;     return 0;
 } 

发表于 2019-02-02 20:09:12 回复(0)
水题
request,cost,preL=map(int,raw_input().split())[1:],0,0
for curL in request:
cost+=(curL-preL)*6 if curL>preL else (preL-curL)*4
preL=curL
cost+=5*len(request)
print cost

编辑于 2019-01-16 19:13:29 回复(0)
lstin = list(map(int,input().split()))
lst = list(lstin)
lst[0] = 0
suma = 0
for i in range(0,len(lst)-1):
    if lst[i]<lst[i+1]:
        suma = suma + 6*(lst[i+1]-lst[i])
    else:
        suma = suma + 4*(lst[i]-lst[i+1])
suma = suma + lstin[0]*5
print(suma)

发表于 2018-11-26 15:33:27 回复(0)
// 思路 题目输入 N 然后输入对应的楼层,问你所有花费的时间
// 比较简单按部就班搬。
#include <iostream>
#include <vector> 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
    int floor;
    int i = 0;
    int originFloor = 0;
    int time = 0; 
    int N;
    cin >> N;
    while((++i) <= N  && cin >> floor)
    {
        if(floor - originFloor >= 0)
        {
            time += (floor - originFloor) * 6;
        }
        else 
        {
            time += (originFloor - floor) * 4; 
        }
        originFloor = floor;
        time += 5;
        //cout << time << endl;
    }
    cout << time << endl;
    return 0;
}

发表于 2018-07-17 16:31:53 回复(0)
#include <queue>
#include <iostream>
using namespace std;

int main(){
	int n, now, total_time=0;
	cin >> n;
	queue<int> request;
	while(n--){
		int u;
		cin >> u;
		request.push(u);
	}
	now = 0;
	while(!request.empty()){
		int next = request.front();
		request.pop();
		if(next == now){
			total_time += 5;
		}
		else if(next > now){
			total_time = total_time + (next-now)*6 + 5;
			now = next;
		}
		else if(next < now){
			total_time = total_time + (now - next)*4 + 5;
			now = next;
		}
	}
	cout << total_time << endl;
	return 0;
}
队列的模拟,不建议使用vector(虽然说用vector也没关系啦。。)
发表于 2017-08-02 02:40:58 回复(0)
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int num = in.nextInt();
		int now = in.nextInt();
		int time = num*5 + (now-0)*6;  
		int next = 0;
		for(int i=1;i<num;i++){
			next = in.nextInt();
			if((next-now)>0)
				time = time + (next-now)*6;
			else if((next-now)<0)
				time = time + (now-next)*4;
			now = next;
		}
		System.out.print(time);
	}
}

发表于 2017-06-15 13:59:21 回复(0)
题目的简单程度让人无法相信了(⊙ˍ⊙)
#include <iostream>
using namespace std;
int floortime(int a, int b)
{
	int sum = 0;
	if (a > b)
		sum = (a - b) * 4;	
	else
		sum = (b - a) * 6;
	return sum;
}
int main()
{
	int length, num;
	while (cin >> length)
	{
		int sum = length * 5, pre = 0;
		for (int i = 0; i < length; i++)
		{
			cin >> num;
			sum += floortime(pre, num);
			pre = num;
		}
		cout << sum << endl;
	}
	return 0;
}

发表于 2017-04-28 16:43:41 回复(0)
#include <iostream>
#include <vector>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

vector<int> order;
int state = 0;
int totalTime = 0;
int sum = 0;
int main(int argc, char** argv) {
	int N;
	cin >> N;
	for(int i = 0;i < N;i++){
		int k;
		cin >> k;
		order.push_back(k);
	}
	for(int i = 0;i < order.end() - order.begin();i++){
		int k = order[i] - state;
		if(k > 0){
			totalTime += 6 * k + 5;
			state = order[i];
		}
		else{
			k = -k;
			totalTime += 4 * k + 5;
			state = order[i];
		}
	}
	cout << totalTime << endl;
	return 0;
}

发表于 2016-01-30 00:13:25 回复(0)
啥头像
requests = map(int, raw_input().strip().split())
requests.pop(0)
time = 0; current = 0
for i in requests:
    if i>current:
        time += ((i-current)*6 + 5)
    elif i < current:
        time += ((current-i)*4 + 5)
    else:
        time += 5
    current = i
print time


发表于 2016-01-27 22:43:06 回复(0)