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 ;
    }

  • 相关阅读:
    【bzoj4066】 简单题
    【bzoj1941】 Sdoi2010—Hide and Seek
    【bzoj2648】 SJY摆棋子
    【poj2154】 Color
    【poj2409】 Let it Bead
    【codevs1106】 篝火晚会
    【poj3270】 Cow Sorting
    【bzoj1004】 HNOI2008—Cards
    【bzoj3143】 Hnoi2013—游走
    【codeforces 749E】 Inversions After Shuffle
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14800203.html
Copyright © 2011-2022 走看看