In this Codechef Car or Bus problem, A man wants to reach home as soon as possible (in minimum time). he has two options first travel by his bike which takes X minutes and second travel with his CAR which takes Y minutes. so we need to determine which option is faster than the other or both options will take some time or not?
Let's say traveling by CAR takes 1 minute and while traveling by bike takes 5 minutes so traveling with the car is faster than a bike.
Problem solution in C programming.
#include <stdio.h>
int main(void) {
// your code goes here
int i,t,x,y;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%d %d",&x,&y);
if(x<y)
{
printf("bike\n");
}
else if(y<x)
{
printf("car\n");
}
else
{
printf("same\n");
}
}
return 0;
}Here in the above code we first declare four integer variables. and then we scan an integer value and store it in the t variable. after that, we run a for loop and scan the test cases and then we check if the value of x is less than y then we will print the bike. and if y is less than x then we will print the car as an output screen else we will print the same on the output screen.
Problem solution in C++ programming.
#include <iostream>
using namespace std;
int main() {
int t,b,c;
cin>>t;
while(t--){
cin>>b>>c;
if(b<c)
cout<<"BIKE";
else if(c<b)
cout<<"CAR";
else
cout<<"SAME";
cout<<endl;
}
// your code goes here
return 0;
}Problem solution in Java programming.
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int x=sc.nextInt();
int y=sc.nextInt();
if(x<y)
{
System.out.println("BIKE");
}else if(x>y)
{
System.out.println("CAR");
}else{
System.out.println("SAME");
}
}
}
}Problem solution in Python programming.
n = int(input())
for i in range(n):
a, b = tuple(map(int, input().split()))
if a < b :
print("Bike")
elif a > b:
print("Car")
else:
print("Same")Here in the above code, we read an integer input and the in the for loop read both the variable as each test case and we check if the value of a is less than b then we will print Bike else if a is greater than b then we will print Car as an output screen else we will print Same as the output screen.

0 Comments