mercredi 5 août 2015

Trying to generate 9 digit numbers with each unique digits

I am trying obtain 9 digit numbers that all have unique digits. My first approach seems a bit too complex and would be tedious to write.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{

int indx;
int num;
int d1, d2, d3, d4, d5, d6, d7, d8, d9;

for(indx = 123456789; indx <= 987654321; indx++)
{

    num = indx;
    d1 = num % 10;
    d2 = ( num / 10 ) % 10;
    d3 = ( num / 100 ) % 10;
    d4 = ( num / 1000 ) % 10;
    d5 = ( num / 10000 ) % 10;
    d6 = ( num / 100000 ) % 10;
    d7 = ( num / 1000000 ) % 10;
    d8 = ( num / 10000000 ) % 10;
    d9 = ( num / 100000000 ) % 10;
    if( d1 != d2 && d1 != d3 && d1 != d3 && d1 != d4 && d1 != d5 && d1 != d6 && d1 != d7 && d1 != d8 && d1 != d9 )
    {
        printf("%d\n", num);
    }
}


}

That is just comparing the first number to the rest. I would have to do that many more to compare the other numbers. Is there a better way to do this?




Aucun commentaire:

Enregistrer un commentaire