Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Permutatore - Permutazioni.c

Permutazioni.c

Caricato da: Netarrow
Scarica il programma completo

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4.  
  5. void permutazioni(int*,int,int);
  6.  
  7. void permutazioni(int elementi[], int index,int dim) {
  8.         register int i,j;
  9.         int app;
  10.  
  11.         if(index >= 1) {
  12.                 for(i=index; i >= 0; i--) {
  13.                 app=elementi[i];
  14.                 elementi[i]=elementi[index];
  15.                 elementi[index] = app;
  16.                 permutazioni(elementi, index-1, dim);
  17.                 app=elementi[i];
  18.                 elementi[i]=elementi[index];
  19.                 elementi[index] = app;
  20.                 }
  21.         }
  22.         else {
  23.                 for(j=0; j < dim; j++)
  24.                         printf("%d", elementi[j]);
  25.                 printf("\n");
  26.         }
  27. }
  28.  
  29.  
  30. int main() {
  31.         int* elementi;
  32.         int nElementi;
  33.         register int i;
  34.  
  35.         printf("%s", "Inserire numero elementi ==> ");
  36.         scanf("%d", &nElementi);
  37.         elementi = (int*) malloc(sizeof(int) * nElementi);
  38.  
  39.         for(i=0; i < nElementi; i++) {
  40.                 printf("%s%d\n", "Inserire elemento ", i+1);
  41.                 scanf("%d", &elementi[i]);
  42.         }
  43.  
  44.     permutazioni(elementi, nElementi-1, nElementi);
  45.  
  46.     #ifdef __WIN32
  47.       system("PAUSE");
  48.     #endif
  49. }