简体   繁体   English

C 计算最大公约数的程序给出错误答案

[英]C program for computing greatest common divisor gives wrong answer

I have written the following program that takes two numbers from the command line and returns the gcd of these numbers.我编写了以下程序,该程序从命令行获取两个数字并返回这些数字的 gcd。

#include <stdio.h>

int to_int(char *input)
{
        return *input - '0';
}

int main(int argc, char *argv[])
{
        if (argc < 2) return 1;
        int a = to_int(argv[1]);
        int b = to_int(argv[2]);
        int i;
        int result;
        for (i = 1; i <= a && i <= b; i++) {
                if (a%i==0 && b%i==0) {
                        result = i;
                }
        }
        printf("%d\n", result);
        return 0;
}

However when I give it the numbers 4 and 16 it returns the answer 1. This is incorrect.但是,当我给它数字 4 和 16 时,它会返回答案 1。这是不正确的。 The gcd of 4 and 16 is 4. However I cannot find what is wrong with my code. 4 和 16 的 gcd 是 4。但是我找不到我的代码有什么问题。 Other examples that I found on the internet seem to be using the same algorithm that I am using (test if both numbers are evenly divisable by i and if they are then the gcd is i).我在互联网上找到的其他示例似乎使用了我正在使用的相同算法(测试两个数字是否可以被 i 整除,如果它们是则 gcd 为 i)。

Could somebody point me towards the mistake in my code?有人可以指出我的代码中的错误吗?

Your to_int function isn't doing what you think.你的to_int function 没有按照你的想法做。

The expression *input - '0' takes the character code of the first character in input and subtracts the character code of '0' .表达式*input - '0'获取input中第一个字符的字符代码并减去'0'的字符代码。 So the result is the number corresponding to the first character of the given string, not the whole string.所以结果是给定字符串的第一个字符对应的数字,而不是整个字符串。

You need to perform this operation in a loop, multiplying the result by 10 before adding the value of the next digit.您需要在循环中执行此操作,将结果乘以 10,然后再添加下一位的值。

Some additional suggestions: (read in-line comments)一些额外的建议:(阅读在线评论)

    // if (argc < 2) return 1;//need argc to be 3
    // (argv[0] always contains program name)
    if (argc < 3) return 1;//test for num arguments
    if(!(is_num(argv[1]) && is_num(argv[2]))) return 1;//test for number
    int a = atoi(argv[1]);//when available use library functions
    int b = atoi(argv[2]);
    int max = a>b?a:b;// determine larger of two inputs
    int i;
    int result= 0;
    //for (i = 1; i <= a && i <= b; i++) {//this is backwards,count from largest 
    for (i = max; i >= 1 ; i--) {//start at max, then decrement i
        if (a%i==0 && b%i==0) {
            result = i;
            break;//break out of loop when solution found
        }
    }
    printf("%d\n", result);
    return 0;

If a support function is desired, create one to verify input is a number:如果需要支持 function,请创建一个以验证输入是否为数字:

bool is_num(char *input)
{
    if(!input) return false;
    int len = strlen(input);
    if(!((input[0] == '-') || (input[0] == '+') || (isdigit(input[0])))) return false;
    for(int i = 1;i<len;i++)
    {
        if(!isdigit(input[i])) return false;
    }
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM