Algorithm
problem link- https://www.spoj.com/problems/GIRLSNBS/
GIRLSNBS - Girls and Boys
There are G girl students and B boy students in a class that is about to graduate. You need to arrange them in a single row for the graduation. To give a better impression of diversity, you want to avoid having too many girls or too many boys seating consecutively. You decided to arrange the students in order to minimize the gender regularity. The gender regularity of an arrangement is the maximum number of students of the same gender (all girls or all boys) that appear consecutively. Given G and B, calculate the minimum gender regularity among all possible arrangements.
Input
Each test case is described using a single line. The line contains two integers G and B representing the number of girls and boys in the class, respectively (0 ≤ G, B ≤ 1000). The end of input is indicated with a line containing the number −1 twice.
Output
For each test case, output a single line with a single integer representing the minimum gender regularity that an arrangement of G girls and B boys can have.
Example
Input:
10 10
5 1
0 1000
-1 -1
Output:
1
3
1000
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <map>
using namespace std;
int main(){
int b=0;
int g=0;
while(true){
cin>>b>>g;
if(b==-1&&g==-1){
break;
}
if(b==0){
cout<<g<<endl;
continue;
}
if(g==0){
cout<<b<<endl;
continue;
}
if(b==g){cout<<"1"<<endl;continue;}
int e=max(b,g)/(min(b,g)+1);
int d=max(b,g)%(min(b,g)+1);
if(d==0)cout<<e<<endl;
else cout<<e+1<<endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
5 1
0 1000
-1 -1
Output
3
1000
Demonstration
SPOJ Solution-Girls and Boys-Solution in C, C++, Java, Python