递归连接两个整数

发布于 2024-10-11 00:40:54 字数 344 浏览 2 评论 0原文

我需要编写一个函数,它接收两个正整数并将它们连接起来返回。

示例: Cat(12,13)​​ 返回 1213

我知道如何以迭代方式执行此操作,它会是这样的:

int Cat(int num1, int num2)
{
     int temp = num2;

     while (temp > 0)
     {
         num1 *= 10;
         temp /= 10;
     }

     return num1 + num2;
}

但是当我使用递归时,我无法使用将用于计算数字的临时变量,并且如果使用该参数我将失去它的价值。

I need to write a function which receives two positive integers and returns them concatenated.

Example: Cat(12,13) returns 1213

I know how to do this the iterative way, it would be something like this:

int Cat(int num1, int num2)
{
     int temp = num2;

     while (temp > 0)
     {
         num1 *= 10;
         temp /= 10;
     }

     return num1 + num2;
}

But when I use recursion I can't use the temporary variable which will be used to count the digits, and if use the parameter I will lose its value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

甲如呢乙后呢 2024-10-18 00:40:54

您可以添加第三个参数来充当某种计数器:

int Cat2(int num1, int num2, int x)
{
     if (x == 0) 
     {
         return num1 + num2;
     }
     else 
     {
         return Cat(num1 * 10, num2, x / 10);
     }
}

int Cat(int num1, int num2)
{
     Cat2(num1, num2, num2)
}

You could add a third parameter to act as a sort of counter:

int Cat2(int num1, int num2, int x)
{
     if (x == 0) 
     {
         return num1 + num2;
     }
     else 
     {
         return Cat(num1 * 10, num2, x / 10);
     }
}

int Cat(int num1, int num2)
{
     Cat2(num1, num2, num2)
}
撩动你心 2024-10-18 00:40:54

这不是“现实生活”的任务,不是吗?无论如何,这是我的主张(递归且没有第三个参数)

int Cat(int num1, int num2)
{
    if(num2 > 0)
    {
        num1 = Cat(num1*10,num2/10);
    }
    return num1 - num2/10 + num2;
}

This is not a "real life" task, is it? Anyway here's my proposition (recursive and without the third parameter)

int Cat(int num1, int num2)
{
    if(num2 > 0)
    {
        num1 = Cat(num1*10,num2/10);
    }
    return num1 - num2/10 + num2;
}
℉絮湮 2024-10-18 00:40:54

您需要递归例程一次处理一位数字,因此调用链将如下所示:

Cat(12, 13)
Cat(121, 3)
Cat(1213, 0) <- at this point the recursion terminates, since num2 == 0

因此您的函数将如下所示:

int Cat(int num1, int num2)
{
    if (num2 == 0)
    {
        return num1;
    }
    else
    {
        // remove most significant digit from num2 and
        // append it to num1
        return Cat(num1, num2);
    }
}

You need your recursive routine to process one digit at a time, so the call chain would look like this:

Cat(12, 13)
Cat(121, 3)
Cat(1213, 0) <- at this point the recursion terminates, since num2 == 0

So your function will look something like this:

int Cat(int num1, int num2)
{
    if (num2 == 0)
    {
        return num1;
    }
    else
    {
        // remove most significant digit from num2 and
        // append it to num1
        return Cat(num1, num2);
    }
}
ˉ厌 2024-10-18 00:40:54

您使用什么语言?您可以简单地将它们转换为字符串并以这种方式连接它们。

What language are you using? you could simply cast them as strings and concatenate them that way.

单身狗的梦 2024-10-18 00:40:54

我不确定你是否把这个练习作为家庭作业——在这种情况下,我要说的可能对你不起作用。

但是,假设您不会将作业问题重新发布到网络上并且您只需要完成此操作,您是否考虑过简单的操作:

  • 在函数入口处将两个整数组合成一个字符串,
  • 转换回整数

在函数出口处将其 (Java伪代码)

int cat(int x, int y) {
 String s = x+""+y;
 return Integer.parseInt(s);
}

I'm not sure if you're engaged in this excercise as homework - in which case what I'm about to say may not work for you.

But assuming you wouldn't re-post your homework question to the Web and You Just Need To Get This Done, have you considered simply:

  • combining the two integers into a string on function entry
  • casting it back to an integer on function exit

for example (in Java pseudo code)

int cat(int x, int y) {
 String s = x+""+y;
 return Integer.parseInt(s);
}
轻许诺言 2024-10-18 00:40:54

为了将 MrGlass 所说的话编入法典,为什么不使用以下代码:

int Cat(int n1, int n2){
    String s1 = Integer.toString(n1);
    String s2 = Integer.toString(n2);

    return Integer.parseInt(s1+s2);
}

To codify what MrGlass said, why not use this code:

int Cat(int n1, int n2){
    String s1 = Integer.toString(n1);
    String s2 = Integer.toString(n2);

    return Integer.parseInt(s1+s2);
}

?

吃颗糖壮壮胆 2024-10-18 00:40:54
int do_cat(int num1, int num2, int temp)
{
    return temp? do_cat(num1 * 10, num2, temp / 10): num1 + num2;
}

int cat(int num1, int num2)
{
    return do_cat(num1, num2, num1);
}
int do_cat(int num1, int num2, int temp)
{
    return temp? do_cat(num1 * 10, num2, temp / 10): num1 + num2;
}

int cat(int num1, int num2)
{
    return do_cat(num1, num2, num1);
}
凝望流年 2024-10-18 00:40:54

为什么要用递归来做呢?

python 中的代码:

from math import log
def concat(a,b):
    return a * 10 ** int(log(b,10)+1) + b

Why do it with recursion anyway?

Code in python:

from math import log
def concat(a,b):
    return a * 10 ** int(log(b,10)+1) + b
一曲爱恨情仇 2024-10-18 00:40:54

这就是它在Scheme中的解决方式:

(define (Cat num1 num2)
    (define (CatLoop num1 num2 temp)
            (if (= temp 0)
                (+ num1 num2)
                (CatLoop (* num1 10) num2 (/ temp 10))))
    (CatLoop num1 num2 num2))

[它可能包含语法错误,我没有测试它。]

在具有嵌套函数的类C语言中:

int Cat(int num1, int num2) {
    int CatLoop(int num1, int num2, int temp) {
        if (temp == 0)
            return num1 + num2;
        else
            return CatLoop(num1 * 10, num2, temp / 10);
    }

    return CatLoop(num1, num2, num2);
}

在尾部调用优化之后,这会展开为以下内容:

int Cat(int num1, int num2) {
    int temp = num2;
    // goto CatLoop;

    CatLoop:
    if (temp == 0)
        goto Done;

    Else:
    num1 *= 10;
    temp /= 10;
    goto CatLoop;

    Done:
    return num1 + num2;
}

This is how it would have been solved in Scheme:

(define (Cat num1 num2)
    (define (CatLoop num1 num2 temp)
            (if (= temp 0)
                (+ num1 num2)
                (CatLoop (* num1 10) num2 (/ temp 10))))
    (CatLoop num1 num2 num2))

[It might contain syntax errors, I didn't test it.]

In a C-like language with nested functions:

int Cat(int num1, int num2) {
    int CatLoop(int num1, int num2, int temp) {
        if (temp == 0)
            return num1 + num2;
        else
            return CatLoop(num1 * 10, num2, temp / 10);
    }

    return CatLoop(num1, num2, num2);
}

After tail-call optimization, this gets unrolled into the following:

int Cat(int num1, int num2) {
    int temp = num2;
    // goto CatLoop;

    CatLoop:
    if (temp == 0)
        goto Done;

    Else:
    num1 *= 10;
    temp /= 10;
    goto CatLoop;

    Done:
    return num1 + num2;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文