Rearranging blocks of digits

戏子无情 提交于 2020-11-29 19:29:29

问题


I encountered a hard question I don't know the answer to: "Rearrange the digits from an integer in blocks of two with a recursive function" here's an example:

Input: 123456

unsigned long pairinvPrint(unsigned long number) {
    printf("%d", number % 100);

    if ((number / 100) <= 99) {
        printf("%d", number / 100);
    }
    else {
        pairinv(number / 100);
    }
}

Output: 563412
More I/O Examples: 42 -> 42; 1234 -> 3412

However, the set circumstances to do this are hard (no loops, arrays, pointers, global- or static variables, no libraries) and it should not print the solution directly, rather return it upon a call like this:

printf("Rearrange int (%lu) = %lu", input, pairinvert(input));

Luckily there's one circumstance to make it easier, the number of the input digits is always even.

Now I experimented for a while, but cant come up with a working solution, except the invalid one using printf.

Does anyone have some inspiration for me or idea how to tackle this?


回答1:


I'll bite :-)

unsigned long p(unsigned long p1, unsigned long p2) {
    // no loops, no arrays, no pointers, no global, no static, no variables, no libraries
    if (p1 < 100) return p2*100 + p1;
    return p(p1/100, p2*100 + p1%100);
}

unsigned long pairinvert(unsigned long n) {
    // no loops, no arrays, no pointers, no global, no static, no variables, no libraries
    if (n < 100) return n;
    return p(n/100, n%100);
}

// need <stdio.h> for printf()
#include <stdio.h>

int main(void) {
    unsigned long input;
    input = 123456;
    printf("Rearrange int (%lu) = %lu\n", input, pairinvert(input));
    input = 42;
    printf("Rearrange int (%lu) = %lu\n", input, pairinvert(input));
    input = 1234;
    printf("Rearrange int (%lu) = %lu\n", input, pairinvert(input));
}



回答2:


Following program should work.

#include <stdio.h>

void rearrange(int n, int *output) {
    int lsd = 0, slsd = 0;

    if(n == 0)
      return;
    if(n > 0) {
        lsd = n%10;
    }
    if (n > 9) {
        slsd = (n%100)/10;
    }

    *output = 100*(*output) + 10*slsd + lsd;

    n = n/100;

    rearrange(n, output);
}

int main() {
    int n;
    int output = 0;

    scanf("%d", &n);
    rearrange(n, &output);
    printf("%d\n", output);

    return 0;
}

It is simple to understand, so I am not writing any comments.

Note that it is tail recursive so with O2 optimization it can recurse infinitely.




回答3:


Try this :

unsigned long pairinv(unsigned long number, unsigned long result) {

    unsigned long n = number % 100;   // Gets the two digit number
    if (n == 0) return result;        // If it's zero returns the result

    result = result * 100 + n;        // Else multiplies the result by 100, adds n
    return pairinv(number / 100, result); // and continues by recursion
}

int main() {
    unsigned long  r= 0;
    printf("%lu\n", pairinv(123456, r)); //==> 563412
    return 0;
}


来源:https://stackoverflow.com/questions/64836465/rearranging-blocks-of-digits

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!