码迷,mamicode.com
首页 > 编程语言 > 详细

(C/C++) Array 印出所有排列組合

时间:2019-04-13 17:42:26      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:i++   div   ret   span   code   col   fine   class   else   

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 
 5 #define N   4
 6 int arr[N] = { 1, 2, 3, 4};
 7 
 8 
 9 void swap(int *a, int *b)
10 {
11   int temp = *a;
12   *a = *b;
13   *b = temp;
14 }
15 
16 void prem(int *arr, int i, int n)
17 {
18   int j;
19   if(i == n)
20   {
21     for(j = 0; j < N; j++) 
22       printf("%d ", arr[j]);
23     printf("\n");
24   }
25   else
26   {
27     for(j = i; j <= n; j++)
28     {
29       swap(&arr[i], &arr[j]);
30       prem(arr, i + 1, n);
31       swap(&arr[i], &arr[j]);
32     }
33   }
34 
35 }
36 
37 int main(void)
38 {
39   int i = 0;
40   prem(arr, 0, N - 1);
41   printf("---------------------\n");
42   for(i = 0; i < N ;i++)
43     printf("%d ", arr[i]);
44   printf("\n");
45 
46   return 0;
47 }

 

(C/C++) Array 印出所有排列組合

标签:i++   div   ret   span   code   col   fine   class   else   

原文地址:https://www.cnblogs.com/ollie-lin/p/10701993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!