Travelling PNR List
A mini bus having S seats can be overbooked during festival times. Out of N bookings made C bookings can be cancelled. Hence the final travelling list has to be prepared for the first S bookings considering the cancellations. Each booking is assigned a PNR. Given the list of PNRs for all the bookings, the program must print the final list of PNRs for the confirmed bookings after cancellation. (The first S bookings remaining after cancellation are considered confirmed)
Input Format:
The first line contains S and N separated by a space.
Next N lines contain the PNRs of the N bookings made.
N+2 th line contains C
Next C lines contain the PNRs of the C bookings cancelled.
Output Format:
S lines contain the PNRs of the confirmed bookings.
Boundary Conditions:
5 <= S <= 30
2 <= N <= 50
N-C > 0
Length of PNR is from 1 to 20
Example Input/Output 1:
Input:
8 10
Q78QJ
BKT6V
32KEW
ETDZI
EH16B
UVZFS
V3ITC
WGGLO
41VJ8
NVIRB
2
UVZFS
BKT6V
Output:
Q78QJ
32KEW
ETDZI
EH16B
V3ITC
WGGLO
41VJ8
NVIRB
Input Format:
The first line contains S and N separated by a space.
Next N lines contain the PNRs of the N bookings made.
N+2 th line contains C
Next C lines contain the PNRs of the C bookings cancelled.
Output Format:
S lines contain the PNRs of the confirmed bookings.
Boundary Conditions:
5 <= S <= 30
2 <= N <= 50
N-C > 0
Length of PNR is from 1 to 20
Example Input/Output 1:
Input:
8 10
Q78QJ
BKT6V
32KEW
ETDZI
EH16B
UVZFS
V3ITC
WGGLO
41VJ8
NVIRB
2
UVZFS
BKT6V
Output:
Q78QJ
32KEW
ETDZI
EH16B
V3ITC
WGGLO
41VJ8
NVIRB
Code:
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner
sc=new Scanner(System.in);
int
i,j,cnt=0;
int
s=sc.nextInt();
int
n=sc.nextInt();
sc.nextLine();
String[]
sa=new String[n];
for(i=0;i<n;i++)
{
sa[i]=sc.nextLine();
}
int
c=sc.nextInt();
sc.nextLine();
String[]
ca=new String[c];
for(i=0;i<c;i++)
ca[i]=sc.nextLine();
for(j=0;j<c;j++)
{
for(i=0;i<n;i++)
{
if(ca[j].equals(sa[i]))
sa[i]="\0";
}
}
for(i=0;i<n;i++)
{
if(sa[i]!="\0")
{
System.out.println(sa[i]);
cnt++;
}
if(cnt==s)
break;
}
}
}
}
Please do comment If u have any Queries!
//RMK
ReplyDeleteimport java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int totalSeats = sc.nextInt();
int totalBooking = sc.nextInt();
ArrayList pnrList = new ArrayList<>();
for(int i=0;i<totalBooking;i++)
pnrList.add(sc.next());
int totalCancelled = sc.nextInt();
for(int i =1;i<=totalCancelled;i++)
{
String temp = sc.next();
for(int j=0;j<pnrList.size();j++)
{
if(temp.equals(pnrList.get(j)))
pnrList.remove(j);
}
}
if(pnrList.size()<totalSeats)
totalSeats = pnrList.size();
for(int i=0;i<totalSeats;i++)
System.out.println(pnrList.get(i));
}
}
Good try dude!
DeleteI read a article under the same title some time ago,community for travelers but this articles quality is much, much better. How you do this..
ReplyDelete