逖靖寒的世界
Share my world. Share your viewpoint.
博客园
社区
首页
新随笔
联系
管理
订阅
随笔- 106 文章- 13 评论- 885
编程实现1到N个数的所有排列组合
编程实现1到N个数的所有排列组合。
如:
n = 3
则得到的序列如下:123, 132, 213, 231, 312, 321
我的实现如下,大家看看如何:
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
5
namespace
ConsoleApplication42
6
{
7
class
Program
8
{
9
static
void
Main(
string
[] args)
10
{
11
int
n
=
4
;
12
13
CreateList creator
=
new
CreateList(n);
14
15
creator.Creat();
16
}
17
}
18
19
public
class
CreateList
20
{
21
private
struct
Num
22
{
23
public
int
num;
24
public
State state;
25
}
26
27
private
enum
State
{ LEFT
=
-
1
, RIGHT
=
1
, STAY
=
0
}
;
28
29
private
int
_n;
30
31
private
int
_totle;
32
33
private
Num[] _nums;
34
35
public
CreateList(
int
n)
36
{
37
if
(n
<
1
)
38
{
39
throw
new
Exception(
"
N should large than zero.
"
);
40
}
41
42
_n
=
n;
43
44
_nums
=
new
Num[_n];
45
46
for
(
int
i
=
0
; i
<
_n; i
++
)
47
{
48
_nums[i].num
=
i
+
1
;
49
_nums[i].state
=
State.LEFT;
50
}
51
52
_totle
=
getTotole(_n);
53
}
54
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
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
76
for
(
int
i
=
0
; i
<
_n; i
++
)
77
{
78
if
(_nums[i].state
==
State.LEFT
&&
i
==
0
)
79
{
80
continue
;
81
}
82
83
if
(_nums[i].state
==
State.RIGHT
&&
i
==
_n
-
1
)
84
{
85
continue
;
86
}
87
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
98
return
index;
99
}
100
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
110
_nums[index1]
=
_nums[index2];
111
_nums[index2]
=
tempNum;
112
}
113
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
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
144
for
(
int
i
=
0
; i
<
_n; i
++
)
145
{
146
numVaule
+=
(System.Int64)(_nums[i].num
*
Math.Pow(
10
, _n
-
i
-
1
));
147
}
148
149
return
numVaule;
150
}
151
152
public
System.Int64 CreatOneNum()
153
{
154
int
largestMoveNumIndex
=
getLargestMoveNumIndex();
155
156
if
(largestMoveNumIndex
!=
-
1
)
157
{
158
int
lastNum
=
_nums[largestMoveNumIndex].num;
159
160
int
index
=
largestMoveNumIndex
+
(
int
)_nums[largestMoveNumIndex].state;
161
162
swap(index, largestMoveNumIndex);
163
164
caluState(lastNum);
165
}
166
167
return
getNumVaule();
168
}
169
170
public
void
Creat()
171
{
172
for
(
int
i
=
0
; i
<
_totle; i
++
)
173
{
174
System.Int64 num
=
CreatOneNum();
175
176
System.Console.WriteLine(num);
177
}
178
179
System.Console.WriteLine(
"
Totle:
"
+
_totle);
180
}
181
}
182
}
183
posted @ 2007-11-17 21:44
逖靖寒
阅读(730)
评论(7)
编辑
收藏
所属分类:
读书
、
算法
发表评论
回复
引用
查看
#1楼
2007-11-21 16:01 |
Silent Void
代码没仔细看,为什么不用递归呢?
回复
引用
查看
#2楼
[
楼主
]2007-11-21 22:03 |
逖靖寒
@Silent Void
如何用递归啊?
回复
引用
#3楼
2007-12-22 14:35 |
bluetooth11111 [未注册用户]
哈哈,楼主注意看了吗?3142和3124出现两次,1234没有出现,.......
回复
引用
查看
#4楼
[
楼主
]2007-12-22 22:18 |
逖靖寒
@bluetooth11111
是。看来程序有错误
回复
引用
#5楼
2008-06-23 01:26 |
12 [未注册用户]
如何下载这个程序阿
回复
引用
查看
#6楼
[
楼主
]
2008-06-23 07:37 |
逖靖寒
@12
没有提供下载,您可以直接copy上面代码直接编译即可。
新用户注册
刷新评论列表
标题
姓名
主页
Email
(博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
网站首页
新闻频道
社区
小组
博问
网摘
闪存
找找看
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索
相关文章:
排列组合计算公式
N70/N72常见问题汇总
编程实现双击某个文件用指定程序打开
相关链接:
所属分类的其他文章:
C#实现遗传算法,模拟花朵的进化。
《Head First HTML with CSS and XHTML》 读后感
动态规划:利用WarShell算法求有向图的传递闭包
时空权衡:利用额外的空间提高字符串匹配的速度
变治法:用C#实现堆的建立与堆排序
如何高效求2个整数的乘积。
如何高效地判断奇数和偶数
编程实现1到N个数的所有排列组合
减治法:C#实现插入排序
分治法:用C#实现快速排序
最新IT新闻:
微软12月举办硬件工程大会 Windows7将首次亮相
电脑程序与12位真人聊天 “艾尔博特”骗过3人
Mozilla发布网页开发工具实验室
经典软件十五年深情回眸
MySpace推自助广告平台MyAds
公告
我的最新闪存
君子不器
10-3 12:47
<
2007年11月
>
日
一
二
三
四
五
六
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
给我留言
查看留言
我管理的小组
Visual Studio
博客园仿真足球交流小组
我参加的小组
程序员音乐空间
.NET 新手小组
设计模式
python
ASP.NET
软件工程师日语
大连.NET俱乐部
Debug 探索团队小组
我参与的团队
大连.NET俱乐部(0/807)
Windows Embedded开发(0/99)
Debug 探索团队(0/53)
随