URI Online Judge Solution 1028 Collectable Cards using C Programming Language.
Input
The first input line contains a single integer N (1 ≤ N ≤ 3000), indicating the number of test cases. Each test case contains two integer numbers F1 (1 ≤ F1 ≤ 1000) and F2 (1 ≤ F2 ≤ 1000) indicating, respectly, the among of collectable cards that Richard and Vicent have to change.
Output
For each test case there will be an integer number at the output, representing the maximum size of the stack of cards which can be exchanged between two players.
Input Sample | Output Sample |
3
8 12 9 27 259 111 |
4
9 37 |
Solution :
#include <stdio.h>
int CollectableCards(int x, int y){
for(;y%x != 0;)
{
int t = x;
x = y%x;
y = t;
}
return x;
}
int main()
{
int i,n, x, y;
scanf("%d",&n);
for(i = 0; i < n; i++)
{
scanf("%d%d",&x,&y);
if(x > y)
{
int t = x;
x = y;
y = t;
}
printf("%d\n", CollectableCards(x,y));
}
return 0;
}
Comments
Post a Comment