Advertisement
VRonin

All Combinations

Feb 24th, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #define MAXIMUM_ELEMENTS 100
  5.  
  6. int main(){
  7.     unsigned int k;
  8.     unsigned int j,h,m,n;
  9.     printf("Inserisci la lunghezza del codice: "); scanf("%u",&k); fflush(stdin);
  10.     unsigned int* Num=(unsigned int*) malloc(MAXIMUM_ELEMENTS*sizeof(unsigned int));
  11.     printf("\nInserisci i numeri che compongono il codice (X per terminare): ");
  12.     unsigned int i=0;
  13.     while(1){
  14.         char Temp;
  15.         scanf("%c",&Temp); fflush(stdin);
  16.         if(Temp=='x' || Temp=='X') break;
  17.         if(Temp<'0' || Temp>'9'){
  18.             printf("\nNon Valido!");
  19.             continue;
  20.         }
  21.         Num[i++]=Temp-'0';
  22.     }
  23.     if(i==0) printf("Nessun Elemento Inserito!");
  24.     else{
  25.         unsigned int NumRighe=pow((double)i,(int)k);
  26.         unsigned int** Result=(unsigned int**) malloc(k*sizeof(unsigned int*));
  27.         for(j=0;j<k;j++){
  28.             Result[j]=(unsigned int*) malloc(sizeof(unsigned int)*(NumRighe));
  29.             for(h=0;h<NumRighe;){ //Per ogni Colonna
  30.                 for(n=0;n<i;n++){ //Per Ogni numero che compone il codice
  31.                     for(m=0;m<pow((double)i,(int)j);m++){
  32.                         Result[j][h++]=Num[n];
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.         for(h=0;h<NumRighe;h++){
  38.             printf("\n");
  39.             for(j=k-1;;j--) {
  40.                 printf("%u",Result[j][h]);
  41.                 if(j==0) break;
  42.             }
  43.         }
  44.  
  45.         for(j=0;j<k;j++) free(Result[j]);
  46.         free(Result);
  47.     }
  48.     free(Num);
  49.     printf("\n"); scanf("%u",&k); //Mette in pausa
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement