开发者

Recursively concatenate two integers

开发者 https://www.devze.com 2023-02-03 20:41 出处:网络
I need to write a function which receives two positive integers and returns them concatenated. Example: Cat(12,13) returns 1213

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开发者_如何学C 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.


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)
}


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;
}


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);
    }
}


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


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);
}


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);
}

?


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);
}


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


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消