题目:不详细

来源:互联网 发布:cms集中管理软件 编辑:程序博客网 时间:2024/05/16 04:50

有字符串如"ABCD,BDAC,DRF,FRD,CADB,CAADB,XB",要将其中由相同字符组成的词归在一起,如果没有和其由相同字符组成的词则去掉,如上的结果为:
组1:ABCD,BDAC,CADB
组2: DRF,FRD
另外,CAADB和XB去掉
请写出代码(c#),思路和选择所使用的数据结构的原因 

 

实现代码:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.ListBox listBox1;
  private System.Windows.Forms.Button button1;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.listBox1 = new System.Windows.Forms.ListBox();
   this.button1 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // listBox1
   //
   this.listBox1.ItemHeight = 12;
   this.listBox1.Location = new System.Drawing.Point(32, 16);
   this.listBox1.Name = "listBox1";
   this.listBox1.Size = new System.Drawing.Size(120, 316);
   this.listBox1.TabIndex = 0;
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(48, 344);
   this.button1.Name = "button1";
   this.button1.TabIndex = 1;
   this.button1.Text = "关闭";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(200, 390);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.listBox1);
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   this.Close();
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   this.fullStrName();
   this.Def();
  }

  private string[] strName;

  private void fullStrName()
  {
   strName = new string[7];
   strName[0]="abcd";
   strName[1]="dbca";
   strName[2]="drf";
   strName[3]="frd";
   strName[4]="cadb";
   strName[5]="caadb";
   strName[6]="xb";
  }

  private void Def()
  {
   int i=0;
   int e=1;

   string StrNameCopy="";

   for(int j=0;j<this.strName.Length ;j++)
   {
    i=1;
    if(this.strName[j]=="")
    {
    }
    else
    {
     StrNameCopy=this.strName[j];
     char[] CH=this.strName[j].ToCharArray();
     char[] SM;
     for(int n=j+1;n<this.strName.Length;n++)
     {
      int m=0;
      SM=this.strName[n].ToCharArray();
 
      if(CH.Length ==SM.Length)
      {
       for(int q=0;q<CH.Length ;q++)
       {
        for(int a=0;a<SM.Length;a++)
        {
         if(CH[q].ToString()==SM[a].ToString())
         {
          m++;
         }
        }
       }
      }

      if(m==CH.Length)
      {
       if(i==1)
       {
        StrNameCopy+=",";
       }
       StrNameCopy+=this.strName[n];
       
       this.strName[n]="";
      }
      
     }

     if(StrNameCopy.ToString().Split(',').Length>1)
     {
      this.listBox1.Items.Add("组"+e.ToString()+":"+ StrNameCopy);
      e++;
     }
     
    }
   }
  }
 }
}

原创粉丝点击