Frage

I'm trying to generate every possible 3-digit combination like: 012, 013 ,014...

BUT: I want to ignore repeated characters (like 999 or 022) and I also don't want to re-use numbers (if 123 already there, don't display 321) so, the last value should be 789.

Here My code :

int main()
 {
    int i;
    int j;
    for(i=1;i<(1<<9);i++)
    {
        for(j=0;j<9;j++)
        {
            if ((1<<j)&i) printf("%d\n",j+1);
        }
    }
 }

I want my result to be ordered like:

012, 013, 014, 015, 016, 017, 018, 019, 023, ..., 789

Also, I'm not supposed to use any function but printf / putchar.

War es hilfreich?

Lösung

I think the easiest way to solve this would be something like

for (i = 0; i <= 7; i++)
{
    for (j = i+1; j <= 8; j++)
    {
        for (k = j+1; k <= 9; k++)
        {
            printf("%d%d%d\n", i, j, k);
        }
    }
}

Andere Tipps

Hello are you from EPITECH ? this is one of the 3rd day of "piscine" work The difficulty of this function is that you cant use printf neither "my_put_nbr" (%d printf)a function that you will have to code tomorrow i guess and who can print int variable so you have to find a way to print each character for each number with my_putchar func for those wondering here is the my_putchar func

void my_putchar(char c)
{
write(1, &c, 1);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top