0
0
Files
42piscine/C_00/git/ex08/ft_print_combn.c
Guy Baconniere d30270c485 commit
2021-08-20 10:02:38 +02:00

86 lines
1.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_combn.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbaconni <marvin@42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/06 09:43:06 by gbaconni #+# #+# */
/* Updated: 2021/08/06 11:38:05 by gbaconni ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putnbr(int nb)
{
char str[10];
int i;
i = 0;
if (nb == 0)
write(1, "0", 1);
while (nb != 0)
{
str[i] = nb % 10 + '0';
nb /= 10;
i++;
}
while (i > 0)
{
--i;
if (str[i] >= 48 && str[i] <= 57)
write(1, &str[i], 1);
}
}
int power(int x, int n)
{
int r;
r = 1;
while (n--)
r *= x;
return (r);
}
int ndigits(int n)
{
int r;
r = 1;
if (n < 0)
{
if (n == 0)
n = 2147483647;
else
n = -n;
}
while (n > 9)
{
n /= 10;
r++;
}
return (r);
}
void ft_print_combn(int n)
{
int x;
int z;
int max;
max = power(10, n) - 1;
x = 1;
while (x <= max)
{
z = n - ndigits(x);
while (z-- > 0)
write(1, "0", 1);
ft_putnbr(x);
if (x != max)
write(1, ", ", 2);
x++;
}
}