Batch GCD
Python
Medium
6 views
Problem Description
Read t pairs a b. For each pair output gcd(a,b).
Input Format
First integer t. Next t lines: a b.
Output Format
t lines gcd.
Sample Test Case
Input:
4
10 15
7 3
0 9
-12 18
Official Solution
import sys,math
p=sys.stdin.read().strip().split()
if not p:
sys.exit(0)
it=iter(p)
t=int(next(it))
out=[]
for _ in range(t):
try:
a=int(next(it)); b=int(next(it))
out.append(str(math.gcd(a,b)))
except Exception:
out.append('0')
sys.stdout.write('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!