forked from love1024/spoj-solution-with-explanation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCD2.cpp
45 lines (35 loc) · 980 Bytes
/
GCD2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<bits/stdc++.h>
using namespace std;
//Find greatest common divisor of two numbers
int gcd(int a,int b) {
if(a == 0)
return b;
return gcd(b%a,a);
}
int main() {
//Take the number of test cases,First number and second
int n,A,B;
//Second number as string
char temp[300];
scanf("%d",&n);
//Loop over all test cases
while(n--) {
//Take first number as integer and second as string
scanf("%d",&A);
scanf("%s",temp);
//If first number is zero simply print the second number
if(A == 0) {
printf("%s\n",temp);
continue;
}
//Else convert second number into integer
//By taking modulus with first number gcd will
//be less than equal to smallest number
B = 0;
for(int i=0;temp[i] != '\0';i++)
B = (B*10 + temp[i]-'0')%A;
//Print the result
printf("%d\n",gcd(A,B));
}
return 0;
}