【C系列4.5】函數訓練之最大公約數 1973
Description
cyn小朋友今天無聊翻數學書的時候看見了最大公約數的定義,他覺得很好奇,於是花了一個下午來琢磨如何判斷兩個數的最大公約數,你能幫她驗算一下嗎?
固定代碼:
Input
第一行輸入一個T,表示有T組數據。
接下來有T行,每行有兩個整數,中間以空格隔開,兩個數都小於10w,求出它們的最大公約數。
Output
對於對於每一對數,輸出他們的最大公約數。
Samples
#include<stdio.h>
int gcd(int x,int y);
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d %d",&n,&m);
printf("%d\n",gcd(n,m));
}
return 0;
}
int gcd(int x, int y) {
int yu;
while (1) {
yu = x % y;
if (0 == yu)
break;
x = y;
y = yu;
}
return y;
}