zoukankan      html  css  js  c++  java
  • c语言 8-8 求最大公约数

    1、

    #include <stdio.h>
    
    int gcd(int x, int y)
    {
        int max, min, tmp;
        if(x != y)
        {
        max = x > y ? x : y;
        min = x > y ? y : x;
        tmp = max % min;
        if(tmp != 0)
            gcd(min, tmp);
        else
            return min;
        }
    }
    
    int main(void)
    {
        int a, b;
        puts("please input two integers.");
        printf("a = "); scanf("%d", &a);
        printf("b = "); scanf("%d", &b);
        
        printf("result: %d
    ", gcd(a, b));
        return 0;
    }

    2、

    #include <stdio.h>
    
    int gcd(int x, int y)
    {
        int max, min, tmp;
        max = x > y ? x : y;
        min = x > y ? y : x;
        tmp = max % min;
        if(tmp != 0)
            gcd(min, tmp);
        else
            return min;    
    } 
    
    int main(void)
    {
        int a, b;
        puts("please input two integers.");
        printf("a = "); scanf("%d", &a);
        printf("b = "); scanf("%d", &b);
        
        printf("result: %d
    ", gcd(a, b));
        
        return 0;
    }

    3、

    #include <stdio.h>
    
    int gcd(int x, int y)
    {
        int max, min, tmp;
        max = x > y ? x : y;
        min = x > y ? y : x;
        tmp = max % min;
        if(tmp != 0)
            gcd(min, tmp);
        else
            return min;
    }
    
    int main(void)
    {
        int a, b;
        puts("please input two integers.");
        do
        {
            printf("a = "); scanf("%d", &a);
            if(a <= 0)
                puts("the range of a is: > 0.");
        }
        while(a <= 0);
        
        do
        {
            printf("b = "); scanf("%d", &b);
            if(b <= 0)
                puts("the rage of b is: > 0");
        }
        while(b <= 0);
        
        printf("the result:  %d
    ", gcd(a, b));
        
        return 0 ;
    }

  • 相关阅读:
    设计模式之简单工厂模式
    设计模式之工厂方法模式
    设计模式之抽象工厂模式
    面向对象设计原则
    Spring与Struts整合
    Spring与Hibernate、Mybatis整合
    Java中执行外部命令
    Spring3之Security
    综合练习:词频统计
    组合数据类型综合练习
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14800203.html
Copyright © 2011-2022 走看看