viewing paste for_bro | C

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_print_combn.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: igomez <[email protected]>              +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2014/08/28 19:01:37 by igomez            #+#    #+#             */
/*   Updated: 2014/08/29 10:05:55 by igomez           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */
 
void ft_print_screen(int *t, int size)
{
    int i;
    int islower;
 
    i = 1;
    islower = 1;
    while (i < size)
    {
        if (t[i - 1] >= t[i])
            islower = 0;
        i++;
    }
    if (islower)
    {
        i = 0;
        while (i < size)
            ft_putchar(t[i++] + '0');
        if (t[0] < (10 - size))
        {
            ft_putchar(',');
            ft_putchar(' ');
        }
    }
}
 
void ft_print_combn(int n)
{
    int i;
    int tab[n];
 
    i = 0;
    if (n == 1)
        while (i < 10)
            ft_putchar(i++ + '0');
    while (i < n)
        tab[i++] = 0;
    while (tab[0] <= (10 - n) && n > 1)
    {
        ft_print_screen(tab, n);
        tab[n - 1]++;
        i = n;
        while (i && n > 1)
        {
            i--;
            if (tab[i] > 9)
            {
                tab[i - 1]++;
                tab[i] = 0;
            }
        }
    }
}
 
Viewed 2440 times, submitted by Guest.