Vladimir
Vladimir

Reputation: 292

Find all available combinations of 3 digits

Please, help me understand how to check that i had same number, but in different order.

void ft_print_comb(void)
{    
    int hun;
    int doz;
    int uni;

    hun = 0;
    doz = 1;
    uni = 2;

    solver(hun, doz, uni);
}
void print( char f, char se, char thi)
{
    ft_putchar(f);
    ft_putchar(se);
    ft_putchar(thi);
    ft_putchar(',');
}
void solver(int x, int y, int z)
{
    while (x < 9){    
        while (y<8){    
            while (z<7){    
                if (x < y && y < z ){
                    print(x, y, z);
                }
                z++;
            }
            y++;
        }
        x++;
    }
}

Create a function that displays all different combinations of three different digits in ascending order, listed by ascending order - yes, repetition is voluntary.

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

987 isn’t there because 789 already is. 999 isn’t there because the digit 9 is present more than once.

Upvotes: 1

Views: 13228

Answers (1)

Serge
Serge

Reputation: 12384

The easiest way is to make sure that you never construct such duplicate numbers. Here is an example:

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

In the above example you will have all possible combinations of non-repeated numbers in the same order. It will always guarantee that i < j < k;

You might find other ways as well.

Upvotes: 5

Related Questions