编程实现1到N个数的所有排列组合
编程实现1到N个数的所有排列组合。
如:
n = 3
则得到的序列如下:123, 132, 213, 231, 312, 321
我的实现如下,大家看看如何:
![](/images/cnblogs_com/gpcuster/eed.jpg)
如:
n = 3
则得到的序列如下:123, 132, 213, 231, 312, 321
我的实现如下,大家看看如何:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](/Images/OutliningIndicators/None.gif)
5
namespace ConsoleApplication42
6
{
7
class Program
8
{
9
static void Main(string[] args)
10
{
11
int n = 4;
12![](/Images/OutliningIndicators/InBlock.gif)
13
CreateList creator = new CreateList(n);
14![](/Images/OutliningIndicators/InBlock.gif)
15
creator.Creat();
16
}
17
}
18![](/Images/OutliningIndicators/InBlock.gif)
19
public class CreateList
20
{
21
private struct Num
22
{
23
public int num;
24
public State state;
25
}
26![](/Images/OutliningIndicators/InBlock.gif)
27
private enum State { LEFT = -1, RIGHT = 1, STAY = 0 };
28![](/Images/OutliningIndicators/InBlock.gif)
29
private int _n;
30![](/Images/OutliningIndicators/InBlock.gif)
31
private int _totle;
32![](/Images/OutliningIndicators/InBlock.gif)
33
private Num[] _nums;
34![](/Images/OutliningIndicators/InBlock.gif)
35
public CreateList(int n)
36
{
37
if (n < 1)
38
{
39
throw new Exception("N should large than zero.");
40
}
41![](/Images/OutliningIndicators/InBlock.gif)
42
_n = n;
43![](/Images/OutliningIndicators/InBlock.gif)
44
_nums = new Num[_n];
45![](/Images/OutliningIndicators/InBlock.gif)
46
for (int i = 0; i < _n; i++)
47
{
48
_nums[i].num = i + 1;
49
_nums[i].state = State.LEFT;
50
}
51![](/Images/OutliningIndicators/InBlock.gif)
52
_totle = getTotole(_n);
53
}
54![](/Images/OutliningIndicators/InBlock.gif)
55
private int getTotole(int n)
56
{
57
if (n == 1)
58
{
59
return n;
60
}
61
else
62
{
63
return n * getTotole(n - 1);
64
}
65
}
66![](/Images/OutliningIndicators/InBlock.gif)
67
/// <summary>
68
/// Find the lagest num index which need move.
69
/// </summary>
70
/// <returns> the index of the lagest num which need move.</returns>
71
private int getLargestMoveNumIndex()
72
{
73
int maxNum = 0;
74
int index = -1;
75![](/Images/OutliningIndicators/InBlock.gif)
76
for (int i = 0; i < _n; i++)
77
{
78
if (_nums[i].state == State.LEFT && i == 0)
79
{
80
continue;
81
}
82![](/Images/OutliningIndicators/InBlock.gif)
83
if (_nums[i].state == State.RIGHT && i == _n - 1)
84
{
85
continue;
86
}
87![](/Images/OutliningIndicators/InBlock.gif)
88
if (_nums[i].state != State.STAY)
89
{
90
if (maxNum < _nums[i].num)
91
{
92
maxNum = _nums[i].num;
93
index = i;
94
}
95
}
96
}
97![](/Images/OutliningIndicators/InBlock.gif)
98
return index;
99
}
100![](/Images/OutliningIndicators/InBlock.gif)
101
/// <summary>
102
/// Swap two nums.
103
/// </summary>
104
/// <param name="index1"> the 1st num's index.</param>
105
/// <param name="index2"> the 2nd num's index.</param>
106
private void swap(int index1, int index2)
107
{
108
Num tempNum = _nums[index1];
109![](/Images/OutliningIndicators/InBlock.gif)
110
_nums[index1] = _nums[index2];
111
_nums[index2] = tempNum;
112
}
113![](/Images/OutliningIndicators/InBlock.gif)
114
/// <summary>
115
/// Calculate nums state which are large than last move num.
116
/// </summary>
117
/// <param name="largestMoveNumIndex"></param>
118
private void caluState(int lastNum)
119
{
120
for (int i = 0; i < _n; i++)
121
{
122
if (_nums[i].num > lastNum)
123
{
124
if (_nums[i].state == State.LEFT)
125
{
126
_nums[i].state = State.RIGHT;
127
}
128
else if (_nums[i].state == State.RIGHT)
129
{
130
_nums[i].state = State.LEFT;
131
}
132
}
133
}
134
}
135![](/Images/OutliningIndicators/InBlock.gif)
136
/// <summary>
137
/// Get the nums vaule.
138
/// </summary>
139
/// <returns>The nums vaule.</returns>
140
private System.Int64 getNumVaule()
141
{
142
System.Int64 numVaule = 0;
143![](/Images/OutliningIndicators/InBlock.gif)
144
for (int i = 0; i < _n; i++)
145
{
146
numVaule += (System.Int64)(_nums[i].num * Math.Pow(10, _n - i - 1));
147
}
148![](/Images/OutliningIndicators/InBlock.gif)
149
return numVaule;
150
}
151![](/Images/OutliningIndicators/InBlock.gif)
152
public System.Int64 CreatOneNum()
153
{
154
int largestMoveNumIndex = getLargestMoveNumIndex();
155![](/Images/OutliningIndicators/InBlock.gif)
156
if (largestMoveNumIndex != -1)
157
{
158
int lastNum = _nums[largestMoveNumIndex].num;
159![](/Images/OutliningIndicators/InBlock.gif)
160
int index = largestMoveNumIndex + (int)_nums[largestMoveNumIndex].state;
161![](/Images/OutliningIndicators/InBlock.gif)
162
swap(index, largestMoveNumIndex);
163![](/Images/OutliningIndicators/InBlock.gif)
164
caluState(lastNum);
165
}
166![](/Images/OutliningIndicators/InBlock.gif)
167
return getNumVaule();
168
}
169![](/Images/OutliningIndicators/InBlock.gif)
170
public void Creat()
171
{
172
for (int i = 0; i < _totle; i++)
173
{
174
System.Int64 num = CreatOneNum();
175![](/Images/OutliningIndicators/InBlock.gif)
176
System.Console.WriteLine(num);
177
}
178![](/Images/OutliningIndicators/InBlock.gif)
179
System.Console.WriteLine("Totle:" + _totle);
180
}
181
}
182
}
183![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
65
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/InBlock.gif)
84
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
90
![](/Images/OutliningIndicators/InBlock.gif)
91
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
95
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
96
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
102
![](/Images/OutliningIndicators/InBlock.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
106
![](/Images/OutliningIndicators/InBlock.gif)
107
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/InBlock.gif)
112
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
113
![](/Images/OutliningIndicators/InBlock.gif)
114
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
115
![](/Images/OutliningIndicators/InBlock.gif)
116
![](/Images/OutliningIndicators/InBlock.gif)
117
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
118
![](/Images/OutliningIndicators/InBlock.gif)
119
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
120
![](/Images/OutliningIndicators/InBlock.gif)
121
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
122
![](/Images/OutliningIndicators/InBlock.gif)
123
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
124
![](/Images/OutliningIndicators/InBlock.gif)
125
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
126
![](/Images/OutliningIndicators/InBlock.gif)
127
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
128
![](/Images/OutliningIndicators/InBlock.gif)
129
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
130
![](/Images/OutliningIndicators/InBlock.gif)
131
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
132
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
133
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
134
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
135
![](/Images/OutliningIndicators/InBlock.gif)
136
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
137
![](/Images/OutliningIndicators/InBlock.gif)
138
![](/Images/OutliningIndicators/InBlock.gif)
139
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
140
![](/Images/OutliningIndicators/InBlock.gif)
141
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
142
![](/Images/OutliningIndicators/InBlock.gif)
143
![](/Images/OutliningIndicators/InBlock.gif)
144
![](/Images/OutliningIndicators/InBlock.gif)
145
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
146
![](/Images/OutliningIndicators/InBlock.gif)
147
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
148
![](/Images/OutliningIndicators/InBlock.gif)
149
![](/Images/OutliningIndicators/InBlock.gif)
150
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
151
![](/Images/OutliningIndicators/InBlock.gif)
152
![](/Images/OutliningIndicators/InBlock.gif)
153
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
154
![](/Images/OutliningIndicators/InBlock.gif)
155
![](/Images/OutliningIndicators/InBlock.gif)
156
![](/Images/OutliningIndicators/InBlock.gif)
157
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
158
![](/Images/OutliningIndicators/InBlock.gif)
159
![](/Images/OutliningIndicators/InBlock.gif)
160
![](/Images/OutliningIndicators/InBlock.gif)
161
![](/Images/OutliningIndicators/InBlock.gif)
162
![](/Images/OutliningIndicators/InBlock.gif)
163
![](/Images/OutliningIndicators/InBlock.gif)
164
![](/Images/OutliningIndicators/InBlock.gif)
165
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
166
![](/Images/OutliningIndicators/InBlock.gif)
167
![](/Images/OutliningIndicators/InBlock.gif)
168
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
169
![](/Images/OutliningIndicators/InBlock.gif)
170
![](/Images/OutliningIndicators/InBlock.gif)
171
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
172
![](/Images/OutliningIndicators/InBlock.gif)
173
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
174
![](/Images/OutliningIndicators/InBlock.gif)
175
![](/Images/OutliningIndicators/InBlock.gif)
176
![](/Images/OutliningIndicators/InBlock.gif)
177
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
178
![](/Images/OutliningIndicators/InBlock.gif)
179
![](/Images/OutliningIndicators/InBlock.gif)
180
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
181
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
182
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
183
![](/Images/OutliningIndicators/None.gif)
![](/images/cnblogs_com/gpcuster/eed.jpg)