S
S
Sergei Sanchez-Perez2021-06-22 11:42:20
C++ / C#
Sergei Sanchez-Perez, 2021-06-22 11:42:20

Create a method that will recombine the digits of two integers, making them one number, taking the digits?

Create a method that will recompose the digits of two integers, making them
one number, taking digits in turn from the first, then from the second number.
The length of the numbers can be different. For example, 123 and 456 will give 142536, 12 and
3456 will give 132456, 1234 and 87 will give 18273456.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-06-22
@SANCHEXAS

For example like this:

int Combine(int a, int b)
{
    var astr = a.ToString();
    var bstr = b.ToString();
    var result = new StringBuilder(astr.Length + bstr.Length);
    for(var i = 0; i < Math.Max(astr.Length, bstr.Length); i++) {
        if (astr.Length > i)
            result.Append(astr[i]);
        if (bstr.Length > i)
            result.Append(bstr[i]);
    }
    return int.Parse(result.ToString());
}

V
Vitaly Kachan, 2021-06-22
@MANAB

https://stackoverflow.com/questions/45508659/get-s...
You get arrays of digits (I usually use modulo 10), then put them together again by multiplying by 10 to the power of the order. Well, you first check the boundary conditions (int32 - 10 digits, the first one is no more than 2 or 4 for unsigned, etc.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question