-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgcd2.c
56 lines (50 loc) · 795 Bytes
/
gcd2.c
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
46
47
48
49
50
51
52
53
54
55
56
#include<stdio.h>
#include<string.h>
#include<math.h>
long gcd(long a, long b)
{
if (b==0)
return a;
else
return gcd(b,a%b);
}
int main()
{
int B_len;
long test_cases,i,j;
long A,B[250],temp;
char buff[255];
long power,remainder,result;
scanf("%ld",&test_cases);
for(i=0;i<test_cases;i++)
{
scanf("%ld",&A);
scanf("%s",&buff);
memset(B,0,250*sizeof(long));
B_len=strlen(buff);
for(j=0;j<B_len;j=j++)
{
B[j]=(buff[j]-'0');
}
if(A!=0)
{
remainder=0;
for(j=0;j<B_len;j++)
{
temp=remainder*10+B[j];
remainder=temp%A;
}
result=gcd(remainder,A);
printf("%ld\n",result);
}
else
{
for(j=0;j<B_len;j++)
{
printf("%ld",B[j]);
}
printf("\n");
}
}
return 0;
}