English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Implementation Method of Combination and Permutation

     C# Implementation Method of Combination and Permutation

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

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

     需求描述:  

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

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

         

    功能实现

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

      1.窗体界面

         窗体界面设计入上面的截图。

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

      3.窗体类代码 

 public partial class FrmDemo : Form 
 {   
 public FrmDemo()    
 {       InitializeComponent();
 } 
 private void FrmDemo_Load(sender, EventArgs e)   
 {    
 string svn = "0b1de737-cea695ab-fc505423-bdd5f3db ";  
 bool ret = ZhuHeNum.Initia(svn);   
 if (!ret)       
 MessageBox.Show("Registration failed!"); 
 } 
 private void btnNot2_Click(sender, EventArgs e)  
 {       string str = txtnot.Text;      
 List<string> list = ZhuHeNum.NumNoRepeAssembly(str, 2); 
 if (list == null)    
 {       
 MessageBox.Show("Registration failed!");     
 return;    
 }     
 list.Sort();     
 StringBuilder sbBuilder = new StringBuilder();   
 foreach (string s in list) 
 {    
 sbBuilder.Append(s);     
 sbBuilder.Append("\r\n");       }  
 txtShow.Clear();      
 txtShow.Text = sbBuilder.ToString(); 
 lbCount.Text = list.Count.ToString();  
 }  
 private void btnNot3_Click(sender, EventArgs e)  
 {      
 string str = txtnot.Text;     
 List<string> list = ZhuHeNum.NumNoRepeAssembly(str, 3);  
 if (list == null)    
 {         MessageBox.Show("Registration failed!");    
 return;     
 }     
 list.Sort();    
 StringBuilder sbBuilder = new StringBuilder();  
 foreach (string s in list)   
 {       
 sbBuilder.Append(s);  
 sbBuilder.Append("\r\n"); 
 }  
 txtShow.Clear();     
 txtShow.Text = sbBuilder.ToString();  
 lbCount.Text = list.Count.ToString();  
 }    
 private void btnAll2_Click(sender, EventArgs e)  
 {       string str = txtAll.Text;     
 List<string> list = ZhuHeNum.NumGroupAssembly(str, 2);  
 if (list == null)   
 {         MessageBox.Show("Registration failed!"); 
 return;     
 }    
 list.Sort();     
 StringBuilder sbBuilder = new StringBuilder(); 
 foreach (string s in list)   
 {        
 sbBuilder.Append(s); 
 sbBuilder.Append("\r\n");       } 
 txtShow.Clear();     
 txtShow.Text = sbBuilder.ToString();  
 lbCount.Text = list.Count.ToString();
 }   
 private void btnAll3_Click(sender, EventArgs e) 
 {      
  
 List<string> list = ZhuHeNum.NumGroupAssembly(str, 3);  
 if (list == null)    
 {        
 MessageBox.Show("Registration failed!");  
 return;     
 }  
 list.Sort();      
 StringBuilder sbBuilder = new StringBuilder();   
 foreach (string s in list)  
 {        
 sbBuilder.Append(s); 
 sbBuilder.Append("\r\n");  
 } 
 txtShow.Clear();      
 txtShow.Text = sbBuilder.ToString(); 
 lbCount.Text = list.Count.ToString(); 
 }  
 }

     4.Running effect

Closing Remarks

            Up to this point, the direct selection and group selection functions have been implemented. AttacheddemoSource code for reference.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report by email to codebox.com (replace # with @ when sending an email) and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like