Advertisement

每日一题(递归类型题目专题)--20200412--1 计算最大公约数 2 求x的n次幂 3 将整型转换为字符串 输入格式 %d%d 提示信息 x=? n=?

阅读量:

基于数学性质的公约数算法设计与修正

复制代码
    #include <stdio.h>
    
    int MaxCommonFactor(int a, int b);
    
    int main()
    {
    int a, b, x;
    printf("Input a,b:");
    scanf("%d,%d", &a, &b);
    x = MaxCommonFactor( a,b);
    if (x < 0)  printf("Input Error!\n");
    printf("%d\n", x);
    }
    
    int MaxCommonFactor(int a, int b)//计算两个正整数的最大公约数
    {
    if (a <= 0 || b <= 0)
        return -1;
     if (a != b)
    {
        if (a > b)
             return MaxCommonFactor(a-b, b);
        else if (b > a)
            return  MaxCommonFactor(b-a, a);
        
    }
    else
    {
        re

全部评论 (0)

还没有任何评论哟~