AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Java Program-Staying In a Hotel

There is a school trip being organized for kids. The hotel the group is staying in can offer them one big room with enough beds to suit any group of visitors, and several smaller rooms with B beds all together. The children have heard many strange and frightening stories about the big room. That´s why not even one of them wants to sleep in the big room. Furthermore not every kid would like to sleep in any bed. Your goal is to assign B beds from the smaller rooms in such a way that the maximal number of children are happy (a child is happy when it gets to sleep in one of the beds it has selected). Input Specification : The first line contains a positive integer t<=1000 indicating the number of test cases. Each test case is an instance of the problem defined above. The first line of each test case is a pair of positive integers L and B (the number of children L<=100 and beds B<=100). The next lines contain a sequence of (c,b) pairs ending with two zeros. (c,b) means that the child c will be happy if it gets to sleep in bed b. Each test case ends when it encounters 0 0. Output Specification : For each test case print the maximal number of happy children. Sample Input: 3 3 3 1 1 2 1 2 2 3 2 3 3 0 0 4 3 1 1 1 3 2 1 3 1 3 2 4 2 0 0 4 2 1 1 1 2 2 1 2 2 3 1 3 2 4 1 4 2 0 0 Sample Output: 3 3 2 Input Explanation: First 3 is number of test case 1st test case is : 3 3-3 children and 3 beds 1 1-child 1 wants bed 1 2 1-child 2 wants bed 1 2 2-child 2 wants bed 2 3 2-child 3 wants bed 2 3 3-child 3 wants bed 3 0 0-both zero means end of this test case 2nd test case is : 4 3-4 children and 3 beds 1 1-child 3 wants bed 3 1 3-child 3 wants bed 3 2 1-child 3 wants bed 3 3 1-child 3 wants bed 3 3 2-child 3 wants bed 3 4 2-child 3 wants bed 3 0 0-both zero means end of this test case 3rd test case is: 4 2-4 children and 2 beds 1 1-child 1 wants bed 1 1 2-child 1 wants bed 2 2 1-child 2 wants bed 1 2 2-child 2 wants bed 2 3 1-child 3 wants bed 1 3 2-child 3 wants bed 2 4 1-child 4 wants bed 1 4 2-child 4 wants bed 2 0 0-both zero means end of this test case Output Explanation: 3-maximal 3 children are happy in 1st test case 3-maximal 3 children are happy in 2nd test case 2-maximal 2 are happy in 3rd test case.

corejava x 336
programming x 153
Posted On : 2016-12-17 14:36:48.0
profile Aarti Kumar - anyforum.in Aarti Kumar
5900
up-rate
5
down-rate

Answers


It´s quite not clear. I think either you missed something in explaining the inputs or in input string. As per my understanding it must be something like this:

import java.util.Scanner;

public class HotelStay {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int t=input.nextInt();
if(t<=1000){
for(int i=1;i<=t;i++){
int max=0;
int c,b=0;
int L=input.nextInt();
int B=input.nextInt();
if(L<=100 && B<=100){
while(true){
c=input.nextInt();
b=input.nextInt();
if(c==0 && b==0){
System.out.print(max+" ");
break;
}
else if(c==b && max<c){
max=c;
}
}
}
}
}
}
}

Posted On : 2016-12-19 01:31:03
Satisfied : 1 Yes  3 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185319307
Reply This Thread
up-rate
-5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in