This article is written by beginners in programming. What's wrong, you should do this more! !! If you have any questions, Please comment.
This question is also a paiza exercise. Please be assured that there is nothing to do with the rank up or down.
Now, m groups of people are coming to their seats in turn. Let a_i be the number of people in the i-th (1 ≤ i ≤ m) group. They try to sit all at once in a series of a_i seats out of the long table seats.
However, since the customer is an Edokko, if any one of those seats is already seated by the previous customer, The whole group gets angry and goes home without sitting down. Edokko is quick.
In the input, the position of consecutive seats on which the i-th group is going to sit is specified by the integer b_i. The i-th group starts from the seat with seat number b_i and tries to sit in a_i seats clockwise from there.
Create a program that outputs the number of people who are safely seated at the long table after the last group has come to sit down.
The input consists of m + 1 lines. On the first line, n (number of seats) and m (number of groups) are entered separated by a half-width space. In the i + 1 line (1 ≤ i ≤ m), two integers a_i (number of people in the group) and b_i (seating start seat number) are entered separated by a half-width space.
One line break is inserted at the end of the last line of the input value. The string is passed from standard input.
Example) 6 3 3 2 1 6 2 5
After the last group comes to sit down, please output the number of people who can be seated safely in one line.
Example) 4
1≦n≦100 1≦m≦100 1≦a_i≦n 1≦b_i≦n
package paiza;
import java.util.*;
public class unagiya {
public static void main(String[] args) {
/*
*Get the total number of seats in the store and the number of groups visiting the store as a String type and store it in line
*Divide the data stored in line by split and store it in lineArray of String array
*/
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
String[] lineArray = line.split(" ");
/*
*Cast the data stored in the lineArray and store it in each variable
*
* seatNum:Number of seats
* totalGuest:Number of groups visiting the store
*/
int seatNum = castString(lineArray[0]);
int totalGuest = castString(lineArray[1]);
/*
* seats:Create the number of seats with hashMap
*Integer is the seat number
*Boolean is the presence or absence of people(If true, you can sit down,)
*/
Map<Integer,Boolean> seats = new HashMap<>();
for(int i = 1; i <= seatNum; i++){
seats.put(i,true);
}
/*
*isSitCustomer:Check if there are any people in the seat chosen by the customer
*If not, throw true
*
* choiceSeat:Seat number that is the base point for customers to choose
*menberNum:Number of customers who came
*/
int choiceSeat = 0;
int memberNum = 0;
for(int i = 0; i < totalGuest; i++){
//Receives console input
//Assign to memberNum and choiceSeat
line = scanner.nextLine();
lineArray = line.split(" ");
memberNum = castString(lineArray[0]);
choiceSeat = castString(lineArray[1]);
//If isSitGuest is true, rewrite the seat information selected by the customer and the number of seats to false.
if(isSitGuest(seats,choiceSeat,memberNum)){
for(int j = 0,index = 1 ; j < memberNum; j++){
if(seats.size() < choiceSeat + j){
seats.put(index, false);
index++;
} else {
seats.put(choiceSeat + j,false);
}
}
}
}
//Check the false number of seat information and assign it to the result
int result = 0;
for(int i = 1; i <= seats.size(); i++){
if(seats.get(i).equals(false)){
result++;
}
}
System.out.println(result);
}
//Cast a String type number to an int type
public static int castString(String strNum){
int num = Integer.parseInt(strNum);
return num;
}
/*
*Map Receive seat information, seats selected by customers, number of customers,
*Check if there is false for the number of customers from the seat selected by the customer
*
*conditions
*Returns false if there are more guests than seats
*Returns false if there is even one false in the seat information
*/
public static boolean isSitGuest(Map seats, int choiceSeat, int menberNum){
if(menberNum > seats.size()){
return false;
}
boolean flag = true;
for(int i = 0,index = 1; i < menberNum; i++){
//index:Specify the very first 1 of the seat information
//Used when counting from 1 when the number of customers exceeds the seat number
if(seats.size() < choiceSeat + i){
if(seats.get(index).equals(false)){
flag = false;
index++;
} else {
index++;
}
} else if(seats.get(choiceSeat + i).equals(false)){
flag = false;
}
}
if(flag){
return true;
} else {
return false;
}
}
}
First, I wrote a rough flowchart
This is a rough idea, but you know what kind of code to write.
The difficult part of this problem is that the seats are circular. There are 10 seats, and a group of 4 customers want to sit from the 8th seat! !! If that is the case 8、9、10、1 It is necessary to process across the seat number.
After thinking for an hour, why not make a counter separately from the counter i for for? That's why I tried this part
for(int j = 0,index = 1 ; j < memberNum; j++){
if(seats.size() < choiceSeat + j){
seats.put(index, false);
index++;
} else {
seats.put(choiceSeat + j,false);
}
}
I also wonder if there is a better way.
First, the nesting of the part that writes false in the seat information is too deep. I can't think of it, but I think I can write it more concisely.
Also, the part that straddles the seat is the same process many times, so I think it should be a method, but Once you solve the problem, your brain won't work ww
The first thing I thought
This was my first post, but this time it's over. If there is a tacit understanding of Qiita, I probably ignore all the cancer, but I would be grateful if anyone who read it could leave a record.
To those who read to the end Thank you very much.
Regarding the exercises, paiza contacted me that I could post.
I tried to solve it in Java! !! Paiza Exercise "Long Table Eel Shop" ~ Object Oriented ~
Recommended Posts