Advertisement
Guest User

Permuta vetores e salva resultado em uma matriz

a guest
Sep 26th, 2011
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. /*
  2. * [permat.c]
  3. * Gera permutacoes de um vetor e as salva
  4. * numa matriz.
  5. *
  6. * [Autor]
  7. * Marcos Paulo Ferreira (Daemonio)
  8. * undefinido gmail com
  9. * http://daemoniolabs.wordpress.com
  10. *
  11. * Seg Set 26 19:41:16 BRT 2011
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. /* Funcao que retorna verdadeiro se
  17.  * `num' nao contem algarismos repetidos
  18.  * e zero caso contrario. */
  19. char eh_sem_repeticao(int *num, int r) {
  20.     int i, j ;
  21.  
  22.     for(i=0; i < r; i++) {
  23.         for(j=0; j < r && i != j; j++) {
  24.             if(num[i] == num[j]) {
  25.                 return 0;
  26.             }
  27.         }
  28.     }
  29.  
  30.     return 1 ;
  31. }
  32.  
  33. int main(int argc, char **argv) {
  34.     int *num ;
  35.     int n ;
  36.     int i, j ;
  37.     int vetor[121][5] ;
  38.     int index_matriz ;
  39.  
  40.     /* Valores que serao permutados. Adicionei
  41.      * eles diretamente para economizar linhas
  42.      * de codigo.*/
  43.     vetor[0][0] = 1 ;
  44.     vetor[0][1] = 7 ;
  45.     vetor[0][2] = 8 ;
  46.     vetor[0][3] = 2 ;
  47.     vetor[0][4] = 3 ;
  48.  
  49.     /* tamanho da entrada. */
  50.     n = 5 ;
  51.  
  52.     /* Aloca espaco para o vetor num. Lembre-se
  53.      * que o vetor `num' representa um numero
  54.      * na base n com n algarismos. */
  55.     num = (int *)calloc(n+1, sizeof(int)) ;
  56.     if ( num == NULL ) {
  57.         perror("malloc") ;
  58.         return -1;
  59.     }
  60.  
  61.     /* Indice da permutacao na matriz. A primeira
  62.      * permutacao tem indice 1, pois o indice 0
  63.      * eh usado para armazenar a entrada. */
  64.     index_matriz = 1 ;
  65.  
  66.     /* Inicio do algoritmo. */
  67.     while ( num[n] == 0 ) {
  68.         for(i=0; i < n; i++) {
  69.             /* Copia permutacoes sem repeticao
  70.              * para o vetor[][] */
  71.             if ( eh_sem_repeticao(num, n) ) {
  72.                 for(j=0; j < n; j++) {
  73.                     vetor[index_matriz][j] = vetor[0][num[j]] ;
  74.                 }
  75.  
  76.                 /* Incrementa o indice da permutacao. */
  77.                 index_matriz++ ;
  78.             }
  79.  
  80.             /* incrementa o algarismo menos
  81.              * significativo. */
  82.             num[0]++ ;
  83.         }
  84.  
  85.         /* distribui os vai-uns. */
  86.         for(i=0; i < n; i++) {
  87.             if(num[i] == n) {
  88.                 num[i] = 0;
  89.                 num[i+1]++ ;
  90.             }
  91.         }
  92.     }
  93.  
  94.     /* Mostra as pemutacoes na tela. */
  95.     for(i=1; i < index_matriz; i++) {
  96.         for(j=0; j < 5; j++) {
  97.             printf("%d ", vetor[i][j]) ;
  98.         }
  99.         putchar('\n') ;
  100.     }
  101.  
  102.     free(num) ;
  103.  
  104.     return 0;
  105. }
  106.  
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement