I had this problem during the day, and it seems to me that I will not go anywhere.
What I want to do:
Create all possible combinations of a nine-digit number between 1-9, but no numbers can be the same. In other words, my goal is to generate exactly 362880 (9!) Numbers, each of which is unique from each other, and each number should contain only one of each digit. There should be no accident.
What I want:
123456789 213796485
What I do not want:
111111111 113456789
What I tried:
I start by creating an array for storing numbers.
float num[9];
Using the principle that I num[0] can be any of 9 digits, and num[8] should be the one that remains, I tried nesting loops. I will send the code, but there is no need to indicate why it does not work, because I already know why. However, I do not 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;
So, as you can see, the last digit will always be 1, the second digit can never be equal to 9, and so on.
So what are my options? I tried to make it so that it would perform a total of 9 ^ 9 times to fix the problem I mentioned, but this, of course, is too inefficient (and it didn’t quite work as intended).
Any ideas? I feel it should be easy to solve, but I can't seem to wrap my head around me.
source share