1973-求最大公約數

【C系列4.5】函數訓練之最大公約數 1973

Time Limit:  1 s      Memory Limit:   32 MB
Submission:225     AC:156     Score:10.00

 

Description

cyn小朋友今天無聊翻數學書的時候看見了最大公約數的定義,他覺得很好奇,於是花了一個下午來琢磨如何判斷兩個數的最大公約數,你能幫她驗算一下嗎?

固定代碼:

#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 ",gcd(n,m));
    }
    return 0;
}

Input

第一行輸入一個T,表示有T組數據。

接下來有T行,每行有兩個整數,中間以空格隔開,兩個數都小於10w,求出它們的最大公約數。

Output

對於對於每一對數,輸出他們的最大公約數。

Samples

input:
3
1 5
4 6
11 22
output:
1
2
11



下附AC代碼:
#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;
}


發佈了53 篇原創文章 · 獲贊 8 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章