Problem Statement
Martha is interviewing at Subway. One of the rounds of the interview requires her to cut a bread of size l×b
into smaller identical pieces such that each piece is a square having
maximum possible side length with no left over piece of bread.
Input format
The first line contains an integerT . T lines follow. Each line contains two space separated integers l and b which denote length and breadth of the bread.
Output format
T
lines, each containing an integer that denotes the number of squares of
maximum size, when the bread is cut as per the given condition.
Constraints
The 1st testcase has a bread whose original dimensions are2×2 , the bread is uncut and is a square. Hence the answer is 1.
The 2nd testcase has a bread of size6×9 . We can cut it into 54 squares of size 1×1 , 0 of size 2×2 , 6 of size 3×3 , 0 of size 4×4 , 0 of size 5×5 and 0 of size 6×6 . The number of squares of maximum size that can be cut is 6.
MY SOLUTION:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int t;
scanf("%d",&t);
while(t--)
{
int l,b,i=2;
scanf("%d %d",&l,&b);
while(l!=1 && b!=1 && i<=b && i<=l)
{
if(l%i==0 && b%i==0)
{
l=l/i;
b=b/i;
}
else
i++;
}
printf("%d\n",l*b);
}
return 0;
}
Input format
The first line contains an integer
Output format
Constraints
1 <= T <= 1000
1 <= l, b <= 1000
Sample Input2
2 2
6 9
Sample Output1
6
ExplanationThe 1st testcase has a bread whose original dimensions are
The 2nd testcase has a bread of size
MY SOLUTION:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int t;
scanf("%d",&t);
while(t--)
{
int l,b,i=2;
scanf("%d %d",&l,&b);
while(l!=1 && b!=1 && i<=b && i<=l)
{
if(l%i==0 && b%i==0)
{
l=l/i;
b=b/i;
}
else
i++;
}
printf("%d\n",l*b);
}
return 0;
}