English 中文(简体)
采用再保险办法的最常见的共同点
原标题:Greatest Common Divisor using recursion
  • 时间:2024-02-07 15:37:47
  •  标签:
  • c

我正试图起草一部法典,以计算两个数字的乘数。

虽然如此,我还是可以印刷数字的分歧。 我正在努力印刷最大的分歧。

#include <stdio.h>

void gcd(int num1, int num2, int i) {
  int x;
    int small = (num1 < num2) ? num1 : num2;
    if (i == small) {
        return;
    } else {
        if (num1 % i == 0 && num2 % i == 0) {
            x = i;
           printf("%d ", x);
        }
        gcd(num1, num2, i + 1);
    }
    
}

int main() {
    int num1, num2, i;
    printf("Enter the 1st number: ");
    scanf("%d", &num1);
    printf("Enter the 2nd number: ");
    scanf("%d", &num2);
    i = 1;
    gcd(num1, num2, i);
    return 0;
}

现在只印刷最大的分歧。 但是,当我管理该方案时,我会随机获得产出(可能会有问题)。 我如何解决这一问题?

#include <stdio.h>

void gcd(int num1, int num2, int i) {
  int x;
    int small = (num1 < num2) ? num1 : num2;
    if (i == small) {
        return;
    } else {
        if (num1 % i == 0 && num2 % i == 0) {
            x = i;
           
        }
        gcd(num1, num2, i + 1);
    }
   printf("%d ", x);
}

int main() {
    int num1, num2, i;
    printf("Enter the 1st number: ");
    scanf("%d", &num1);
    printf("Enter the 2nd number: ");
    scanf("%d", &num2);
    i = 1;
    gcd(num1, num2, i);
    return 0;
}
问题回答

我的档案。 Don t知道我从哪里获得。

unsigned int gcd(unsigned int a, unsigned int b) {
  if(b) return(gcd(b, a % b));
  return(a);
}

Problems

我的理解是,在你的第一部法典中,你想考虑人数从1个减少到1个,找到共同的分歧。 但是,当你使用<代码>if (i=小型)时,职能<代码>gcd 当<代码>i等于小数时,将返回。 这意味着,如果某一数字确实是另一个编号(例如:<代码>6和12/code>)的,则该功能为t t输出全球升温潜能值。 因此,请使用<><<>>>条码>if (i > small>

Another problem in your code is in your second code, as pointed in the comment section, the value of x is undefined, so the program may output undefined values (e.g. 2097184), and that s why you get random outputs. Therefore you should probably initialize x, like int x = 0;.

Solution

我建议你使用一种具有回报价值的功能,因为收益价值可以与其他计算结合起来,并产生可运输性。

One of the solution is just find the common divisors, and then find the greatest one.
You can still use your function gcd(int num1, int num2, int i), but you can let it to have a return value means the greatest number from i to the smaller number that is the common divisor of the two numbers, and the return value 0 means the is no number meet the conditions above.
The source code:

#include <stdio.h>

int gcd(int num1, int num2, int i) {
    int x = 0, y;
    int small = (num1 < num2) ? num1 : num2;
    if (i > small) {
        return 0;
    } else {
        if (num1 % i == 0 && num2 % i == 0) {
            x = i;
        }
        y=gcd(num1,num2,i + 1);
        return (x > y) ? x : y;
    }
}

int main() {
    int num1, num2, i;
    printf("Enter the 1st number: ");
    scanf("%d", &num1);
    printf("Enter the 2nd number: ");
    scanf("%d", &num2);
    i = 1;
    printf("%d",gcd(num1, num2, i));
    return 0;
}

解决这一问题的另一种方法是使用comment节所述Euclid算法,可在以下声明中表述:

gcd(a, b) = gcd(b, a%b)(a >=b)

So, we can use a gcd(int num1, int num2) function and let the gcd function to return gcd(num2, num1 % num2) when num1 >= num2.
But what to do when one of the numbers is 0? We return the other number.
Based on the content above, we can write the following source code:

#include <stdio.h>

int gcd(int num1, int num2) {
    if (num1 == 0) {
        return num2;
    } else if (num2 == 0) {
        return num1;
    } else {
        return gcd(num2, num1 % num2);
    }
}

int main() {
    int num1, num2;
    printf("Enter the 1st number: ");
    scanf("%d", &num1);
    printf("Enter the 2nd number: ");
    scanf("%d", &num2);
    printf("%d",gcd(num1, num2));
    return 0;
}

Conclusion

Some possible solutions of the problem are listed above.
I think that it would will be easier to write and understand if you use loops to solve the problem. Of course, it would be good for you to understand other methods and the logic thoughts behind.

幼儿发展中心(Euclid: 大约300BC):

#include <stdio.h>

int gcd(int a, int b)
{
    int rem = a % b;
    return rem != 0
        ? gcd(b, rem)
        : b;
} /* gcd */

int main()
{
#define P(a, b)                        
        printf("P(%d, %d) -> %d
",    
                   a,  b,    gcd(a, b))
    P(8, 6);
    P(8, 16);
    P(7, 24);
} /* main */

I cannot guess where did you get what you write in your question. My apologies for it.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签