★★枚举法求最大公约数

Q1697.(语言: C)按如下函数原型,采用穷举法编写计算最大公约数的函数Gcd(),在主函数中调用该函数计算并输出从键盘任意输入的两整数的最大公约数。穷举法的基本思想是:先找到a和b中的较小者t,然后从t开始逐次减1尝试每种可能,即检验t到1之间的所有整数,第一个满足公约数条件的t,就是a和b的最大公约数。
/* 函数功能:计算a和b的最大公约数,输入负数时返回-1 */
int Gcd(int a, int b)
要求如下:
(1)从键盘任意输入的两整数
主函数调用Gcd()函数,并输出两整数的最大公约数。
(2)Gcd函数原型为:
int Gcd(int a, int b);
如果输入的数不是正整数,则返回-1,否则,返回两个数的最大公约数。
(3)**输入提示信息格式要求:"Input a,b:"
输入格式:"%d,%d"
**输出提示信息要求:
如果输入的数不是正整数,则输出"Input number should be positive!\n",否则按如下格式输出"Greatest Common Divisor of %d and %d is %d\n"

#include<stdio.h>

int gcd(int a,int b){
    if(a<=0||b<=0){
        return -1;
    }
    int t = a>b?b:a;
    while(t){
        if(a%t==0&&b%t==0){
            return t;
        }
        t--;
    }
}

int main()
{
    int a,b,ret;
    printf("Input a,b:");
    scanf("%d,%d",&a,&b);
    ret = gcd(a,b);
    if(ret==-1){
        printf("Input number should be positive!\n");
    }else{
        printf("Greatest Common Divisor of %d and %d is %d\n",a,b,ret);
    }
    return 0;
}

posted @ 2026-03-06 20:29  沫忆拾忆  阅读(13)  评论(0)    收藏  举报