Seating Arrangement | Basics of Input/Output

Problem Statement :
Akash and Vishal are quite fond of travelling. They mostly travel by railways. They were travelling in a train one day and they got interested in the seating arrangement of their compartment. The compartment looked something like


So they got interested to know the seat number facing them and the seat type facing them. The seats are denoted as follows : 
  • Window Seat : WS
  • Middle Seat : MS
  • Aisle Seat : AS
You will be given a seat number, find out the seat number facing you and the seat type, i.e. WSMS or AS.

Problem Link :
https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/seating-arrangement-1/

INPUT
First line of input will consist of a single integer T denoting number of test-cases. Each test-case consists of a single integer N denoting the seat-number.

OUTPUT : 
For each test case, print the facing seat-number and the seat-type, separated by a single space in a new line.

CONSTRAINTS :
  • 1<=T<=105
  • 1<=N<=108
Solution :

// Code here


#include<bits/stdc++.h> 
using namespace std;
 int main()  
 {  
      int n,t,ch;  
      cin>>t;       // for test cases
      for(int i=1;i<=t;i++)  
      {  
           cin>>n;    // seat number
           ch=n%12;   
           switch(ch)  
           {  
                case 0 : n-=11;  
                           cout<<n<<" WS"<<endl;  
                           break;  
                case 1 : n+=11;  
                           cout<<n<<" WS"<<endl;  
                           break;  
                case 2 : n+=9;  
                           cout<<n<<" MS"<<endl; 
                           break;  
                case 3 : n+=7;  
                           cout<<n<<" AS"<<endl;  
                           break;  
                case 4 : n+=5;  
                           cout<<n<<" AS"<<endl;  
                           break;  
                case 5 : n+=3;  
                           cout<<n<<" MS"<<endl;  
                           break;  
             case 6 : n+=1;  
                           cout<<n<<" WS"<<endl;  
                           break;  
                case 7 : n-=1;  
                           cout<<n<<" WS"<<endl; 
                           break;  
                case 8 : n-=3;  
                           cout<<n<<" MS"<<endl; 
                           break;  
                case 9 : n-=5;  
                           cout<<n<<" AS"<<endl;  
                           break;  
             case 10 : n-=7;  
                           cout<<n<<" AS"<<endl; 
                           break;  
             case 11 : n-=9;  
                           cout<<n<<" MS"<<endl;  
                           break;  
           }  
      }  
      return 0;  
 }

Code Snippet - 1

Code Snippet - 2

Submission
Solution Explained :
We use variable t to store test cases. For each test case we take seat number n. Now if we look at the arrangement of each compartment, there are 12 seats in each compartment which have a specific order and associated seat type. So we declare a variable ch, which is basically n%12...this will give us the specific seat position in a compartment. Now finally we switch the ch variable for 12 values for different seat type. One which get matched, we adjust the seat number n for it accordingly and print the Seat Number n and Seat Type accordingly.
This completes our Program.
Do comment your response plz.

FOR ANY QUERY REGARDING SOLUTION
free feel to contact
Rahul Badgujar
WhatsApp : +917775919753
Email: rahulrb15899@gmail.com


Comments

Popular posts from this blog

Prime Number | Basics of Input/Output

Palindromic String | Basics of Input/Output