命题:
将一个数组的全排列输出,数据无素不重复(暂不考虑重复的情况.)
如:定一个这样一个数
int a[] = new a[4]{1,2,3,4};
输出结果:
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321
我现在只是假定数组数据是1234,原理是一样的。实现程序如下:
代码如下(C#):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1
int
count
=
0
;
2 List < int > list = new List < int > ();
3 int Max = 4 ;
4 private void CalcCycle()
5 {
6 if (list.Count == 4 )
7 {
8 string text = "" ;
9 for ( int j = 0 ; j < Max; j ++ )
10 { text += list[j].ToString(); }
11 Console.WriteLine(text + " \r\n " );
12 count ++ ; // 总数+1
13 }
14 else
15 for ( int i = 0 ; i < Max; i ++ )
16 {
17 int a = i + 1 ;
18 if ( ! list.Contains(a))
19 {
20 list.Add(a);
21 CalcCycle();
22 list.Remove(a);
23 }
24 }
25 }
2 List < int > list = new List < int > ();
3 int Max = 4 ;
4 private void CalcCycle()
5 {
6 if (list.Count == 4 )
7 {
8 string text = "" ;
9 for ( int j = 0 ; j < Max; j ++ )
10 { text += list[j].ToString(); }
11 Console.WriteLine(text + " \r\n " );
12 count ++ ; // 总数+1
13 }
14 else
15 for ( int i = 0 ; i < Max; i ++ )
16 {
17 int a = i + 1 ;
18 if ( ! list.Contains(a))
19 {
20 list.Add(a);
21 CalcCycle();
22 list.Remove(a);
23 }
24 }
25 }