[Solved] Minimum Travel Time with Java, C++, Python

Minimum Travel Time: The renowned book fair of the season “Publishers Federation Book Expo” is back, it promises to be bigger and better with a spread of about a million books on display. It is organized in a wide space this year on the topmost floor N of Hotel Grand Regency.

Williams, an ardent book lover visits the fair and wants to minimize the time it takes him to go from the N-th floor to ground floor. He can either take the elevator or the stairs.
The stairs are at an angle of 45 degrees and Williams’s velocity is V1 m/s when taking the stairs down. The elevator on the other hand moves with a velocity V2 m/s. Whenever an elevator is called, it always starts from ground floor and goes to N-th floor where it collects Williams (collecting takes no time), it then makes its way down to the ground floor with Williams in it.
The elevator cross a total distance equal to N meters when going from N-th floor to ground floor or vice versa, while the length of the stairs is sqrt(2) * N because the stairs is at angle 45 degrees. Williams has requested your help to decide whether he should use stairs or the elevator to minimize his travel time. Can you help him out?

Input Format:
First line of the input contains three space-separated integers N, V1, V2.

Output Format:
Output a single line with string Elevator or Stairs, denoting the answer to the problem.
Refer sample input and output for formatting specifications.

Sample Input 1:
5 10 15

Sample Output 1:
Elevator

Sample Input 2:
2 10 14

Sample Output 2:
Stairs

Solution

import java.util.*;
import java.lang.*;
import java.io.*;


class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{

		Scanner s = new Scanner(System.in);

		   	int n = s.nextInt();
		   	int v1 = s.nextInt();
		   	int v2 = s.nextInt();
		   	//sauravhathi
		   	double d1 =Math.pow(2,0.5)*n;
		   	double d2 = 2*n;
		   	double t1=d1/v1;
		   	double t2 = d2/v2;
		   	if(t1>t2)
		   	{
		   	    System.out.println("Elevator");
		   	}
		   	else{
		   	    System.out.println("Stairs");
		   	}
	}
}
#include <bits/stdc++.h>
using namespace std;

int main() {
	    int n,v1,v2;
	    cin>>n>>v1>>v2;
	    //sauravhathi
	    double a=(double(n)/double(v2))*2;
	    double b=(n*(sqrt(2))/v1);
	    
	    if(a<b) cout<<"Elevator"<<endl;
	    else cout<<"Stairs"<<endl;
	return 0;
}
import math  as m
n,v1,v2=list(map(int,input().split()))
k=m.sqrt(2)*n
l=n*2

# sauravhathi
t=k/v1
o=l/v2
if t>o:
    print("Elevator")
else:
    print("Stairs")

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *