#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>
typedef int* nombre;
typedef int chiffre;
int verbose = 0;
void affiche (nombre z,int max)
  { int i;
    for(i= max - 1 ;i >= 0 ;i--)
       printf(" %d", z[i] );
   }

nombre zero (int max)
  {
  return (nombre) calloc (max, sizeof( chiffre ));
  }
   
int plusun (nombre N,int b,int n )
   {
   int i = 0;
   if ( verbose ) {
     printf("\n");
     affiche( N, n );
   }
   while ( (i < n) && (N[i] == b-1)) {
	N[i] = 0;
	i = i+1;
   }
   if (i < n)
	 N[i] = N[i] + 1;

   if ( verbose ) {
     printf(" -> ");
     affiche( N, n );
     printf(" cout: %d", i);
   }
    return i;
   }
 
void output(  int base , int pas , int max)
{ int nb, cpt, total, qte;
  nombre N;
  printf("\nbase=%d max=%d pas=%d", base, max, pas);
  for ( nb = pas; nb <= max; nb+= pas ) {
	N = zero ( nb );
	total = 0;
	qte = 0;
        do {
           cpt = plusun (N, base, nb);
           total += cpt;
	   qte ++;
	   
        } while ( cpt < nb );
	printf("\ntaille=%3d total=%7d moy=%.2f", nb, total, (float) total / qte);
  }
}

int main (int argc ,char*argv[])
  {
  int base=2, pas=1, nb, max=5;
  int opt;
  char * optliste = "p:b:m:h:v";
  while (( opt = getopt( argc, argv, optliste )) >= 0 ) {
      switch ( opt ) { 
       case 'p' : pas  = atoi(optarg);   break; 
       case 'b' : base = atoi(optarg);   break;   
       case 'm' : max  = atoi(optarg);   break;
       case 'v' : verbose = 1;           break; 
       case 'h' : printf("\nusage  %s: -p pas -m max -b base -verbose", argv[0]);
	 printf("\ndefaut   : pas=1  max=5 base=2 verbose=0");
       default  : exit(1); 
      }
  }
  output( base, pas, max);
  printf("\neoj...\n");
  return 0; 
  }
