首页 > 代码库 > 组合排列的实现方法
组合排列的实现方法
最近在做数据分析系统,里面涉及到组合排列的问题,查找了很多的资料,但是感觉很多资料都是比较零散的,达不到项目需求。
后来经过一段的时间的探索,终于实现了组合排列的功能。下面我就来简单说说吧。
需求描述:
要实现的功能就是字符或数字的组合排列。例如: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源码下载
组合排列的实现方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。