简体   繁体   English

在C中逐个数字地对两个数字求和

[英]Summing two numbers recursively digit by digit in C

I need to write to recursive functions, where in the first one, I need to sum two integers digit by digit. 我需要写递归函数,在第一个函数中,我需要逐位求和两个整数。 I have written some code, but it gives me the final result multiplied by 10. I see that the problem happens because when I sum first two digits I multiply them by 10. 我已经写了一些代码,但是它给了我最终的结果乘以10。我看到了问题的发生,因为当我将前两位数字相加时,我会将它们乘以10。

The second function must count number of carries in the sum. 第二个功能必须计算总和中的进位数。 Meaning if two digits were 3 and 8, then when we sum them we get 11, which is result 1, and carry 1. Simply, I just need to count how many carries occur. 意思是如果两个数字分别是3和8,那么当我们将它们相加时,得到11,即结果1,并且进位为1。简单地说,我只需要计算发生多少进位。

Please note that I assume that both numbers have same number of digits. 请注意,我假设两个数字的位数相同。

#include <stdio.h>

int sum(int a, int b)
{
    int temp = (a%10) + (b%10);
    static int mul = 1;

    if(a == 0 && b == 0)
        return 0;
    else
    {
        mul *= 10;
        return (mul*temp) + sum(a/10, b/10);
    }
}

int carry(int a, int b)
{
    static int counter = 0;

    if((a%10) + (b%10) > 9)
        counter++;

    if(a == 0 && b == 0)
        return counter;

    carry(a/10, b/10);

}

int main()
{
    int a = 1941;
    int b = 2282;

    int result = sum(a, b);
    printf("%d\n", result);

    int car = carry(a, b);
    printf("%d\n", car);

    return 0;
}
return (mul*temp) + sum(a/10, b/10);

Should be: 应该:

return temp + 10*sum(a/10, b/10);

You don't need a static variable, Static variables are used to implement a global state whose lifetime extent the entire process. 您不需要静态变量, 静态变量用于实现全局状态,该状态的整个生命周期范围。 That's not something desirable and should only be used at last resort. 这不是可取的,只能在不得已时使用。 Furthermore, that's definitely not what you need here, using static variables to implement your solution will leads to functions that only work the first time they are called. 此外,这里绝对不是您需要的,使用静态变量来实现您的解决方案将导致仅在首次调用它们时起作用的函数。

You should use the recursive property of your algorithm to aggregate the result: 您应该使用算法的递归属性来聚合结果:

int sum(int a, int b)
{
    if(a == 0 && b == 0) {
        return 0;
    }
    else
    {
        return (a%10) + (b%10) + 10*sum(a/10, b/10);
    }
}

sum(1941, 2282) will expand to : sum(1941, 2282)将扩展为:

sum(1941, 2282)
1 + 2 + 10*sum(194, 228)
1 + 2 + 10*(4 + 8 + 10*sum(19, 22))
1 + 2 + 10*(4 + 8 + 10*(9 + 2 + 10*sum(1, 2))
1 + 2 + 10*(4 + 8 + 10*(9 + 2 + 10*(1 + 2 + 10*sum(0, 0))
1 + 2 + 10*(4 + 8 + 10*(9 + 2 + 10*(1 + 2 + 10*0)

You should use the same approach for carry : 您应该对carry使用相同的方法:

int carry(int a, int b)
{
    if(a == 0 && b == 0) {
        return 0;
    }
    else if((a%10) + (b%10) > 9) {
        return 1 + carry(a/10, b/10);
    }
    else {
        return carry(a/10, b/10);
    }
}

carry(1941, 2282) will expand to : carry(1941, 2282)将扩展为:

carry(1941, 2282)
0 + carry(194, 228)
0 + 1 + carry(19, 22)
0 + 1 + 1 + carry(1, 2)
0 + 1 + 1 + 0 + carry(0, 0)
0 + 1 + 1 + 0 + 0

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

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