Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate a series of number with unique digits in C?

Tags:

c

I've been at this problem for a day now and it feels like I'm getting nowhere.

What I want to do:

Generate all possible combinations of a nine-digit number between 1-9, but no digits can be the same. In other words, my goal is to generate exactly 362880 (9!) numbers, each one unique from one another and each number must contain only one of each digits. There should be no randomness involved.

What I want:

123456789
213796485

What I DON'T want:

111111111
113456789

What I've tried:

I start by creating an array to store the digits.

float num[9];

Using the principle that I num[0] can be any of the 9 digits, and num[8] has to be the one remaining, I tried nesting loops. I'll post the code, but there's no need to point out why it doesn't work because I already know why. However, I don't know how to fix it.

for (int a = 1; a < 10; a++) {
    num[0] = a;
    for (int b = 1; b < 9; b++) {
        if (b != a)
            num[1] = b;

        // The code in between follows the same pattern
        for (int i = 1; i < 2; i++) {
            if (i != a && i != b && i != c && i != d && i != e && i != f && i != g && i != h) {
                num[8] = i;
            }
        }
    }
}

So as you can see, the last digit will always be 1, the second digit can never be 9 and so on.

So what options do I have? I tried making it so that it loops a total of 9^9 times, which would fix the problem I mentioned, but that's of course way too inefficient (and it didn't quite work as intended either).

Any ideas? I feel like it should be an easy thing to solve but I can't seem to be able to wrap my head around it.

like image 527
Lobs001 Avatar asked Oct 29 '22 11:10

Lobs001


1 Answers

Here is a simple solution that generates the 362880 permutations in lexicographical order:

#include <stdio.h>
#include <string.h>

void perm9(char *dest, int i) {
    if (i == 9) {
        printf("%.9s\n", dest);
    } else {
        for (char c = '1'; c <= '9'; c++) {
            if (memchr(dest, c, i) == NULL) {
                dest[i] = c;
                perm9(dest, i + 1);
            }
        }
    }
}

int main(void) {
    char dest[9];
    perm9(dest, 0);
    return 0;
}
like image 119
chqrlie Avatar answered Nov 15 '22 05:11

chqrlie