组合排列的实现方法

  最近在做数据分析系统,里面涉及到组合排列的问题,查找了很多的资料,但是感觉很多资料都是比较零散的,达不到项目需求。

后来经过一段的时间的探索,终于实现了组合排列的功能。下面我就来简单说说吧。

需求描述:  

   要实现的功能就是字符或数字的组合排列。例如:ab 的所有组合为:ab,ba ;  ab的所有不重复排列为:ab。

其实这也是彩票中常说的直选和组选。效果图如下:

  功能实现

  这里就不多说了,直接贴上实现代码吧。

1.窗体界面设计

设计代码:

  1 partial class FrmDemo
  2     {
  3         /// <summary>
  4         /// 必需的设计器变量。
  5         /// </summary>
  6         private System.ComponentModel.IContainer components = null;
  7
  8         /// <summary>
  9         /// 清理所有正在使用的资源。
 10         /// </summary>
 11         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
 12         protected override void Dispose(bool disposing)
 13         {
 14             if (disposing && (components != null))
 15             {
 16                 components.Dispose();
 17             }
 18             base.Dispose(disposing);
 19         }
 20
 21         #region Windows 窗体设计器生成的代码
 22
 23         /// <summary>
 24         /// 设计器支持所需的方法 - 不要
 25         /// 使用代码编辑器修改此方法的内容。
 26         /// </summary>
 27         private void InitializeComponent()
 28         {
 29             this.label5 = new System.Windows.Forms.Label();
 30             this.btnAll2 = new System.Windows.Forms.Button();
 31             this.btnAll3 = new System.Windows.Forms.Button();
 32             this.groupBox3 = new System.Windows.Forms.GroupBox();
 33             this.txtAll = new System.Windows.Forms.TextBox();
 34             this.label4 = new System.Windows.Forms.Label();
 35             this.btnNot3 = new System.Windows.Forms.Button();
 36             this.btnNot2 = new System.Windows.Forms.Button();
 37             this.txtnot = new System.Windows.Forms.TextBox();
 38             this.groupBox2 = new System.Windows.Forms.GroupBox();
 39             this.groupBox1 = new System.Windows.Forms.GroupBox();
 40             this.label3 = new System.Windows.Forms.Label();
 41             this.label1 = new System.Windows.Forms.Label();
 42             this.txtShow = new System.Windows.Forms.TextBox();
 43             this.lbCount = new System.Windows.Forms.Label();
 44             this.groupBox3.SuspendLayout();
 45             this.groupBox2.SuspendLayout();
 46             this.groupBox1.SuspendLayout();
 47             this.SuspendLayout();
 48             //
 49             // label5
 50             //
 51             this.label5.AutoSize = true;
 52             this.label5.Location = new System.Drawing.Point(13, 35);
 53             this.label5.Name = "label5";
 54             this.label5.Size = new System.Drawing.Size(65, 12);
 55             this.label5.TabIndex = 20;
 56             this.label5.Text = "输入内容:";
 57             //
 58             // btnAll2
 59             //
 60             this.btnAll2.Location = new System.Drawing.Point(71, 76);
 61             this.btnAll2.Name = "btnAll2";
 62             this.btnAll2.Size = new System.Drawing.Size(90, 35);
 63             this.btnAll2.TabIndex = 15;
 64             this.btnAll2.Text = "2组合";
 65             this.btnAll2.UseVisualStyleBackColor = true;
 66             this.btnAll2.Click += new System.EventHandler(this.btnAll2_Click);
 67             //
 68             // btnAll3
 69             //
 70             this.btnAll3.Location = new System.Drawing.Point(165, 76);
 71             this.btnAll3.Name = "btnAll3";
 72             this.btnAll3.Size = new System.Drawing.Size(90, 35);
 73             this.btnAll3.TabIndex = 11;
 74             this.btnAll3.Text = "3组合";
 75             this.btnAll3.UseVisualStyleBackColor = true;
 76             this.btnAll3.Click += new System.EventHandler(this.btnAll3_Click);
 77             //
 78             // groupBox3
 79             //
 80             this.groupBox3.Controls.Add(this.label5);
 81             this.groupBox3.Controls.Add(this.btnAll2);
 82             this.groupBox3.Controls.Add(this.txtAll);
 83             this.groupBox3.Controls.Add(this.btnAll3);
 84             this.groupBox3.Location = new System.Drawing.Point(22, 189);
 85             this.groupBox3.Name = "groupBox3";
 86             this.groupBox3.Size = new System.Drawing.Size(369, 144);
 87             this.groupBox3.TabIndex = 18;
 88             this.groupBox3.TabStop = false;
 89             this.groupBox3.Text = "取所有组合";
 90             //
 91             // txtAll
 92             //
 93             this.txtAll.Location = new System.Drawing.Point(85, 32);
 94             this.txtAll.Name = "txtAll";
 95             this.txtAll.Size = new System.Drawing.Size(239, 21);
 96             this.txtAll.TabIndex = 12;
 97             this.txtAll.Text = "abc012";
 98             //
 99             // label4
100             //
101             this.label4.AutoSize = true;
102             this.label4.Location = new System.Drawing.Point(13, 30);
103             this.label4.Name = "label4";
104             this.label4.Size = new System.Drawing.Size(65, 12);
105             this.label4.TabIndex = 19;
106             this.label4.Text = "输入内容:";
107             //
108             // btnNot3
109             //
110             this.btnNot3.Location = new System.Drawing.Point(167, 59);
111             this.btnNot3.Name = "btnNot3";
112             this.btnNot3.Size = new System.Drawing.Size(90, 35);
113             this.btnNot3.TabIndex = 18;
114             this.btnNot3.Text = "3组合";
115             this.btnNot3.UseVisualStyleBackColor = true;
116             this.btnNot3.Click += new System.EventHandler(this.btnNot3_Click);
117             //
118             // btnNot2
119             //
120             this.btnNot2.Location = new System.Drawing.Point(71, 59);
121             this.btnNot2.Name = "btnNot2";
122             this.btnNot2.Size = new System.Drawing.Size(90, 35);
123             this.btnNot2.TabIndex = 17;
124             this.btnNot2.Text = "2组合";
125             this.btnNot2.UseVisualStyleBackColor = true;
126             this.btnNot2.Click += new System.EventHandler(this.btnNot2_Click);
127             //
128             // txtnot
129             //
130             this.txtnot.Location = new System.Drawing.Point(85, 27);
131             this.txtnot.Name = "txtnot";
132             this.txtnot.Size = new System.Drawing.Size(239, 21);
133             this.txtnot.TabIndex = 16;
134             this.txtnot.Text = "abc012";
135             //
136             // groupBox2
137             //
138             this.groupBox2.Controls.Add(this.label4);
139             this.groupBox2.Controls.Add(this.btnNot3);
140             this.groupBox2.Controls.Add(this.btnNot2);
141             this.groupBox2.Controls.Add(this.txtnot);
142             this.groupBox2.Location = new System.Drawing.Point(22, 32);
143             this.groupBox2.Name = "groupBox2";
144             this.groupBox2.Size = new System.Drawing.Size(369, 131);
145             this.groupBox2.TabIndex = 19;
146             this.groupBox2.TabStop = false;
147             this.groupBox2.Text = "取不重复组合";
148             //
149             // groupBox1
150             //
151             this.groupBox1.Controls.Add(this.groupBox2);
152             this.groupBox1.Controls.Add(this.groupBox3);
153             this.groupBox1.Location = new System.Drawing.Point(291, 39);
154             this.groupBox1.Name = "groupBox1";
155             this.groupBox1.Size = new System.Drawing.Size(416, 364);
156             this.groupBox1.TabIndex = 16;
157             this.groupBox1.TabStop = false;
158             this.groupBox1.Text = "操作";
159             //
160             // label3
161             //
162             this.label3.AutoSize = true;
163             this.label3.Location = new System.Drawing.Point(155, 11);
164             this.label3.Name = "label3";
165             this.label3.Size = new System.Drawing.Size(41, 12);
166             this.label3.TabIndex = 15;
167             this.label3.Text = "总数:";
168             //
169             // label1
170             //
171             this.label1.AutoSize = true;
172             this.label1.Location = new System.Drawing.Point(24, 11);
173             this.label1.Name = "label1";
174             this.label1.Size = new System.Drawing.Size(65, 12);
175             this.label1.TabIndex = 14;
176             this.label1.Text = "显示结果:";
177             //
178             // txtShow
179             //
180             this.txtShow.Location = new System.Drawing.Point(26, 39);
181             this.txtShow.Multiline = true;
182             this.txtShow.Name = "txtShow";
183             this.txtShow.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
184             this.txtShow.Size = new System.Drawing.Size(217, 364);
185             this.txtShow.TabIndex = 12;
186             //
187             // lbCount
188             //
189             this.lbCount.AutoSize = true;
190             this.lbCount.Location = new System.Drawing.Point(202, 11);
191             this.lbCount.Name = "lbCount";
192             this.lbCount.Size = new System.Drawing.Size(41, 12);
193             this.lbCount.TabIndex = 13;
194             this.lbCount.Text = "label2";
195             //
196             // FrmDemo
197             //
198             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
199             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
200             this.ClientSize = new System.Drawing.Size(724, 414);
201             this.Controls.Add(this.groupBox1);
202             this.Controls.Add(this.label3);
203             this.Controls.Add(this.label1);
204             this.Controls.Add(this.txtShow);
205             this.Controls.Add(this.lbCount);
206             this.Name = "FrmDemo";
207             this.Text = "凹凸组合测试";
208             this.Load += new System.EventHandler(this.FrmDemo_Load);
209             this.groupBox3.ResumeLayout(false);
210             this.groupBox3.PerformLayout();
211             this.groupBox2.ResumeLayout(false);
212             this.groupBox2.PerformLayout();
213             this.groupBox1.ResumeLayout(false);
214             this.ResumeLayout(false);
215             this.PerformLayout();
216
217         }
218
219         #endregion
220
221         private System.Windows.Forms.Label label5;
222         private System.Windows.Forms.Button btnAll2;
223         private System.Windows.Forms.Button btnAll3;
224         private System.Windows.Forms.GroupBox groupBox3;
225         private System.Windows.Forms.TextBox txtAll;
226         private System.Windows.Forms.Label label4;
227         private System.Windows.Forms.Button btnNot3;
228         private System.Windows.Forms.Button btnNot2;
229         private System.Windows.Forms.TextBox txtnot;
230         private System.Windows.Forms.GroupBox groupBox2;
231         private System.Windows.Forms.GroupBox groupBox1;
232         private System.Windows.Forms.Label label3;
233         private System.Windows.Forms.Label label1;
234         private System.Windows.Forms.TextBox txtShow;
235         private System.Windows.Forms.Label lbCount;
236     }

折叠展开

2.添加引用

在项目中,添加引用——>AoTu.ZhuHe.dll

3.窗体类代码

  1 public partial class FrmDemo : Form
  2     {
  3         public FrmDemo()
  4         {
  5             InitializeComponent();
  6         }
  7
  8         private void FrmDemo_Load(object sender, EventArgs e)
  9         {
 10             string svn = "0b1de737-cea695ab-fc505423-bdd5f3db ";
 11             bool ret = ZhuHeNum.Initia(svn);
 12             if (!ret)
 13                 MessageBox.Show("注册失败!");
 14         }
 15
 16         private void btnNot2_Click(object sender, EventArgs e)
 17         {
 18             string str = txtnot.Text;
 19             List<string> list = ZhuHeNum.NumNoRepeAssembly(str, 2);
 20             if (list == null)
 21             {
 22                 MessageBox.Show("注册失败!");
 23                 return;
 24             }
 25             list.Sort();
 26             StringBuilder sbBuilder = new StringBuilder();
 27
 28             foreach (string s in list)
 29             {
 30                 sbBuilder.Append(s);
 31                 sbBuilder.Append("\r\n");
 32             }
 33             txtShow.Clear();
 34             txtShow.Text = sbBuilder.ToString();
 35             lbCount.Text = list.Count.ToString();
 36         }
 37
 38         private void btnNot3_Click(object sender, EventArgs e)
 39         {
 40             string str = txtnot.Text;
 41             List<string> list = ZhuHeNum.NumNoRepeAssembly(str, 3);
 42             if (list == null)
 43             {
 44                 MessageBox.Show("注册失败!");
 45                 return;
 46             }
 47             list.Sort();
 48             StringBuilder sbBuilder = new StringBuilder();
 49
 50             foreach (string s in list)
 51             {
 52                 sbBuilder.Append(s);
 53                 sbBuilder.Append("\r\n");
 54             }
 55             txtShow.Clear();
 56             txtShow.Text = sbBuilder.ToString();
 57             lbCount.Text = list.Count.ToString();
 58         }
 59
 60         private void btnAll2_Click(object sender, EventArgs e)
 61         {
 62             string str = txtAll.Text;
 63             List<string> list = ZhuHeNum.NumGroupAssembly(str, 2);
 64             if (list == null)
 65             {
 66                 MessageBox.Show("注册失败!");
 67                 return;
 68             }
 69             list.Sort();
 70             StringBuilder sbBuilder = new StringBuilder();
 71
 72             foreach (string s in list)
 73             {
 74                 sbBuilder.Append(s);
 75                 sbBuilder.Append("\r\n");
 76             }
 77             txtShow.Clear();
 78             txtShow.Text = sbBuilder.ToString();
 79             lbCount.Text = list.Count.ToString();
 80         }
 81
 82         private void btnAll3_Click(object sender, EventArgs e)
 83         {
 84             string str = txtAll.Text;
 85             List<string> list = ZhuHeNum.NumGroupAssembly(str, 3);
 86             if (list == null)
 87             {
 88                 MessageBox.Show("注册失败!");
 89                 return;
 90             }
 91             list.Sort();
 92             StringBuilder sbBuilder = new StringBuilder();
 93
 94             foreach (string s in list)
 95             {
 96                 sbBuilder.Append(s);
 97                 sbBuilder.Append("\r\n");
 98             }
 99             txtShow.Clear();
100             txtShow.Text = sbBuilder.ToString();
101             lbCount.Text = list.Count.ToString();
102         }
103     }

展开折叠

4.运行效果

结束语

至此,直选和组选功能就实现了。附上demo源码供参考。

Demo源码下载

时间: 2024-10-06 09:37:27

组合排列的实现方法的相关文章

LightOJ 1005 Rooks(组合排列)或(dp,还得再看看)

n*n的棋盘中,放k个棋子,每个棋子不能同行同列 1 #include<cstdio> 2 #include<iostream> 3 #include<queue> 4 #include<string> 5 #include<math.h> 6 #include<stack> 7 #include<cstdlib> 8 #include<set> 9 #include<map> 10 #includ

递归求解几类排列组合问题(一、类循环组合排列)

对于搜索的深度很深或深度不固定的情况,则无法用枚举的方法来设置循环嵌套的层数,这时可以考虑用递归法来完成搜索任务.递归是一种常用算法,它是搜索的另一种实现方式.如果在算法设计中采用一个函数或过程直接或间接地调用它自身来解决问题的方法,则称该方法为递归算法.递归算法必须要设计好一个或若干个确定的递归终止条件. 一.类循环组合排列 Sample Input : 4 2 Sample Output 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 10

递归求解几类排列组合问题(二、全组合排列)

对于搜索的深度很深或深度不固定的情况,则无法用枚举的方法来设置循环嵌套的层数,这时可以考虑用递归法来完成搜索任务.递归是一种常用算法,它是搜索的另一种实现方式.如果在算法设计中采用一个函数或过程直接或间接地调用它自身来解决问题的方法,则称该方法为递归算法.递归算法必须要设计好一个或若干个确定的递归终止条件. Sample Input 3 1 2 3 Sample Output 123 132 213 231 312 321 #include<stdio.h> #include<strin

递归求解几类排列组合问题(三、非重复组合排列)

三.非重复组合排列(含重复数字时,生成不重复组合排列) 对于搜索的深度很深或深度不固定的情况,则无法用枚举的方法来设置循环嵌套的层数,这时可以考虑用递归法来完成搜索任务.递归是一种常用算法,它是搜索的另一种实现方式.如果在算法设计中采用一个函数或过程直接或间接地调用它自身来解决问题的方法,则称该方法为递归算法.递归算法必须要设计好一个或若干个确定的递归终止条件. Sample Input 4 1 2 2 3 Sample Output 1223 1232 1322 2123 2132 2213

递归求解几类排列组合问题(四、普通选择性组合排列)

四.普通选择性组合排列 对于搜索的深度很深或深度不固定的情况,则无法用枚举的方法来设置循环嵌套的层数,这时可以考虑用递归法来完成搜索任务.递归是一种常用算法,它是搜索的另一种实现方式.如果在算法设计中采用一个函数或过程直接或间接地调用它自身来解决问题的方法,则称该方法为递归算法.递归算法必须要设计好一个或若干个确定的递归终止条件. Sample Input 5 3 1 2 3 4 5 Sample Output 123 124 125 134 135 145 234 235 245 345 #i

PHP数组内容不重复组合排列算法

最近在做ecshop的商品库存模块,分别给一款商品的多个属性组合设置库存,如下图: 一款手机有不同颜色,屏幕尺寸,系统和电量,都要设置不同的库存,如果都要手动选择属性组合,则会耗费很多不必要的时间.假如打开页面时就已经设置好属性排列组合那就最好不过,因此想了整天,写了如下函数: 1 <?php 2 3 /* 4 Author:GaZeon 5 Date:2016-6-20 6 Function:getArrSet 7 Param:$arrs 二维数组 8 getArrSet(array(arra

递归算法之类循环组合排列

输入: 给定数据,输入n,m.分别代表位数,可能取到的值. 输出: 输出所有可能的数字. 样例输入: 4 2 样例输出: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 题目分析: 这是一种典型的递归算法-------类递归组合排列. 代码如下: <span style="font-size:14px;">#include<stdio.h> int n

数组组合排列及排序

//组合排列let array = ['1','2','3'] function getGroup(data, index = 0, group = []) { let need_apply = []; need_apply.push(data[index]); for (var i = 0; i < group.length; i++) { need_apply.push(group[i] + ' ' + data[index]); } group.push.apply(group, need

itertools库 combinations() 和 permutations() 组合 和 排列选项的方法

combinations方法重点在组合,permutations方法重在排列. combinations和permutations返回的是对象地址,原因是在python3里面,返回值已经不再是list,而是iterators(迭代器), 所以想要使用,只用将iterator 转换成list 即可 原文地址:https://www.cnblogs.com/kaibindirver/p/10714375.html