Co-Partners in Train: Tim and Bob are off to a famous Education Fair “Knowledge Forum 2017” at Uzhlanda. This time they have to travel without their guardians. Tim got very interested in the arrangement of seats inside the train coach.
The entire coach could be viewed as an arrangement of consecutive blocks of size 8.
BerthNumber | Compartment |
1-8 | 1 |
9-16 | 2 |
17-24 | 3 |
… and so on |
Each of these size-8 blocks are further arranged as:
1LB, 2MB, 3UB, 4LB, 5MB, 6UB, 7SL, 8SU |
9LB, 10MB, … |
……. |
……. |
Here LB denotes lower berth, MB middle berth and UB upper berth.
The following berths are called Co-Partners in Train:
3 UB | 6 UB |
2 MB | 5 MB |
1 LB | 4 LB |
7 SL | 8 SU |
and the pattern is repeated for every set of 8 berths.
Tim and Bob are playing this game of finding the co-partner in train of each berth. Write a program to do the same.
Input Format:
The input consists of an integer N, which corresponds to the berth number whose neighbor is to be found out.
Output Format:
The output is to display the berth of the neighbor of the corresponding seat.
Refer sample input and output for formatting specifications.
Sample Input 1:
1
Sample Output 1:
4LB
Sample Input 2:
5
Sample Output 2:
2MB
Solution
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
if(n%8>0&&n%8<7)
{
int r = n%8;
if(r<=3)
{
n=n+3;
if(n%8==4)
{
System.out.println(n+"LB");
}
//sauravhathi
else if(n%8==5)
{
System.out.println(n+"MB");
}
else
{
System.out.println(n+"UB");
}
}
else
{
n=n-3;
if(n%8==1)
{
System.out.println(n+"LB");
}
else if(n%8==2)
{
System.out.println(n+"MB");
}
else
{
System.out.println(n+"UB");
}
}
}
else
{
int r=n%8;
if(r==7)
{
n=n+1;
System.out.println(n+"SU");
}
else
{
n=n-1;
System.out.println(n+"SL");
}
}
}
}
#include <stdio.h>
int main(){
int n,d,o;
scanf("%d",&n);
d = n%8;
if(d!=0){
if(d<4){
o = n+3;
}else if (d!=7){
o=n-3;
}
}
switch(d){
case 0:
printf("%dSL",n-1);
break;
case 1: case 4:
printf("%dLB",o);
break;
case 2: case 5:
printf("%dMB",o);
break;
case 3: case 6:
printf("%dUB",o);
break;
case 7:
printf("%dSU",n+1);
}
return 0;
}
Happy Learning – If you require any further information, feel free to contact me.