I've been at this problem for a day now and it feels like I'm getting nowhere.
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
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With