Attachment 'perm.c'

Download

   1 #include <stdio.h>
   2 #include <stdlib.h>
   3 
   4 void swap( int *x, int *y)
   5 {
   6     int t;
   7     t  = *x;
   8     *x = *y;
   9     *y =  t;
  10 }
  11 
  12 void perm( int *m, int j, int n )
  13 {
  14     int k, i;
  15     if (j==(n-1)) {
  16         for( k=0; k<n; k++)
  17           printf( "%d ", m[k]);
  18         printf( "\n");
  19     } else
  20     {
  21         for( i=j; i<=n-1; i++ ) {
  22           swap(m+j, m+i);
  23           perm(m,j+1,n);x
  24           swap(m+j, m+i);
  25         }
  26     }
  27 }
  28 
  29 
  30 int main (int argc, char **argv){
  31     int i, n;
  32     int *m;
  33     
  34     scanf( "%d", &n );
  35     m = malloc(n*sizeof(int));
  36     
  37     for(i=0; i<n; i++)
  38       m[i] = i+1; /* 1, 2, 3, ... n */
  39       
  40     perm(m, 0, n);  
  41     
  42     free(m);
  43 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.