Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Co-partner in Train
Tim and Bob are off to the wedding of a close relative. This time they have to travel without their guardians. Rahul 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.

Berth Number   	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 Train-Partners:

3UB   |  6UB  
2MB   |  5MB  
1LB   |  4LB  
7SL   |  8SU  
and the pattern is repeated for every set of 8 berths.

Tim and bob are playing this game of finding the co-partner train partner of each berth. Can you write a program to do the same?

Input
The input consists of an integer N, which corresponds to the berth number whose neighbor is to be found out.

Output
The output is to display the berth of the neighbor of the corresponding seat.

Sample test cases
Input 1                                                   Output 1
  1                                                         4LB
  5                                                         2MB


What I have tried:

import java.util.Scanner;

public class Main {
	static final int[] PARTNER_BERTH_NUMBER = { 3, 4, 5, 0, 1, 2, 7, 6 };
	static final String[] PARTNER_BERTH_CODE = { "LB", "MB", "UB", "LB", "MB", "UB", "SU", "SL" };

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int T = sc.nextInt();
		for (int tc = 0; tc < T; tc++) {
			int N = sc.nextInt();

			System.out.println(solve(N));
		}

		sc.close();
	}

	static String solve(int N) {
		int compartment = (N - 1) / 8;
		int berthNumber = (N - 1) % 8;

		return String.format("%d%s", compartment * 8 + PARTNER_BERTH_NUMBER[berthNumber] + 1,
				PARTNER_BERTH_CODE[berthNumber]);
	}
}
Posted
Updated 20-Sep-22 23:42pm
Comments
CHill60 21-Feb-22 7:20am    
What is wrong with the code that you have tried?
OriginalGriff 21-Feb-22 8:31am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900