using System;

namespace ConsoleApplication2
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 class Class1
 {
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
   //
   // TODO: 在此处添加代码以启动应用程序
   //
   string star=Console.ReadLine();
   // SortAll(star);
   // Console.ReadLine();
   CombAll(star);
   SortAll(star);
   Console.ReadLine();
  }
  #region  全排列
  static void SortAll(string star)
  {
   string intStar="|";
   for(int i=0;i<star.Length ;i++)
   {
    intStar+=i.ToString()+"|";
   }
   Sort(star,intStar,"|");
  }

  static void Sort(string star,string intStar,string StrIn)
  {
   string result="|";
   int i=star.Length ;
   if(StrIn.Length ==intStar.Length )
   {
    string[] temp=StrIn.Split('|');
    string StrOut="";
    for(int k=1;k<star.Length+1 ;k++)
    {
     int t=Convert.ToInt32(temp[k]);
     StrOut+=star.Substring(t,1);
    }
    Console.WriteLine(StrOut);
   }
   else
   {
    #region 开始循环取值
    for(int j=0;j<i;j++)
    {
     string mid="";
     string[] temp=intStar.Split('|');
     mid=temp[j+1];
     //mid=intStar.Substring(j,1);
     if(StrIn.IndexOf("|"+mid+"|")>=0)
     {
     }
     else
     {
      bool flag=true;
      //判断后面的字符是否用过
      //bool flag0=true;

      //mid对应的字符
      string mid0=star.Substring(j,1);
      //已经构成排列的数字字符串数组。
      string[] ok=StrIn.Split ('|');
      //依次取后面的字符
      for(int k=j+1;k<i;k++)
      {
       if(StrIn.IndexOf("|"+k.ToString()+"|")>=0)  //用过
       {
       }
       else  //没有用过
       {
        if(mid0==star.Substring(k,1))  //有相同的
        {
         flag=false;
         break;
        }
        else
        {
         flag=true;
        }
       }       
      }
      if(flag)
      {
       result=StrIn+mid+"|";
       Sort(star,intStar,result);
      }
     }
    } 
    #endregion
   }
  }
  #endregion
  #region 全组合
  static void CombAll(string star)
  {
   string intStar="|";
   for(int i=0;i<star.Length ;i++)
   {
    intStar+=i.ToString()+"|";
   }
   for(int i=1;i<star.Length +1;i++)
   {
    Combination(star,"|","",i);
   }
  }
  /// <summary>
  /// 给定字符,给定长度(长度小于给定字符串的长度),列出所有组合
  /// 即给定的字符不能重复使用
  /// </summary>
  /// <param name="star">给定的初始字符串</param>
  /// <param name="INstartar">给定的初始字符串</param>
  /// <param name="l">组合后的字符串长度</param>
  ///
  static void Combination(string star,string IntIn,string StrIn,int l)
  {
   if(StrIn.Length >=l)
   {
    if(StrIn.Length ==l)
    {
     Console.WriteLine(StrIn);
    }
   }
   else
   {
    #region 开始循环取值
    for(int j=0;j<star.Length ;j++)
    {  
     string mid="";
     bool flag=true;
     mid=star.Substring(j,1);
     if(IntIn.IndexOf("|"+j.ToString()+"|")>=0)
     {
     }
     else
     {
      
      for(int k=j+1;k<star.Length ;k++)  //一次取后面的字符
      {
       if(IntIn.IndexOf("|"+k.ToString()+"|")>=0) //用过
       {
       }
       else
       {
        if(mid==star.Substring(k,1))
        {
         flag=false;
         break;
        }
        else
        {
         flag=true;
        }
       }
      }   //一次取后面的字符
      
      if(flag)
      {
       bool lastflag=false;
       if(IntIn.Length >1)
       {
        int Intpre0=IntIn.LastIndexOf("|");
        string tempIntIn=IntIn.Remove(Intpre0,1);
        int Intpre1=tempIntIn.LastIndexOf("|");
        string Intpre=IntIn.Substring(Intpre1+1,Intpre0-Intpre1-1);

        int intPre=Convert.ToInt32(Intpre);
        if(intPre>j)
        {
         lastflag=true;
        }
        else
        {
         if(intPre<j)
         {
          lastflag=false;
         }
         else
         {
          if(mid==StrIn.Substring(StrIn.Length-1,1))
          {
           lastflag=true;
          }
          else
          {
           lastflag=false;
          }
         }
        }
       }
       else
       {
        lastflag=true;
       }

       if(lastflag)
       {
        string IntInTemp=IntIn+j.ToString()+"|";
        //IntIn+=j.ToString()+"|";
        string StrInTemp=StrIn+mid;
        //StrIn+=mid;
        Combination(star,IntInTemp,StrInTemp,l);
       }
      }
     }
     
      
    }
    #endregion
   }
  }
  #endregion
  /// <summary>
  /// 给定字符,给定长度(长度小于给定字符串的长度),列出所有排列
  /// 即给定的字符不能重复使用
  /// </summary>
  /// <param name="star"></param>
  /// <param name="IntIn"></param>
  /// <param name="StrIn"></param>
  /// <param name="l"></param>
  ///
  
  static void Sort(string star,string IntIn,string StrIn,int l)
  {
   if(StrIn.Length >=l)
   {
    if(StrIn.Length ==l)
    {
     Console.WriteLine(StrIn);
    }
   }
   else
   {
    #region 开始循环取值
    for(int j=0;j<star.Length ;j++)
    {  
     string mid="";
     bool flag=true;
     mid=star.Substring(j,1);
     if(IntIn.IndexOf("|"+j.ToString()+"|")>=0)
     {
     }
     else
     {
      
      for(int k=j+1;k<star.Length ;k++)  //一次取后面的字符
      {
       if(IntIn.IndexOf("|"+k.ToString()+"|")>=0) //用过
       {
       }
       else
       {
        if(mid==star.Substring(k,1))
        {
         flag=false;
         break;
        }
        else
        {
         flag=true;
        }
       }
      }   //一次取后面的字符
      
      if(flag)
      {
       /*
       bool lastflag=false;
       if(IntIn.Length >1)
       {
        int Intpre0=IntIn.LastIndexOf("|");
        string tempIntIn=IntIn.Remove(Intpre0,1);
        int Intpre1=tempIntIn.LastIndexOf("|");
        string Intpre=IntIn.Substring(Intpre1+1,Intpre0-Intpre1-1);

        int intPre=Convert.ToInt32(Intpre);
        if(intPre>j)
        {
         lastflag=true;
        }
        else
        {
         if(intPre<j)
         {
          lastflag=false;
         }
         else
         {
          if(mid==StrIn.Substring(StrIn.Length-1,1))
          {
           lastflag=true;
          }
          else
          {
           lastflag=false;
          }
         }
        }
       }
       else
       {
        lastflag=true;
       }
       */
       //if(lastflag)
       //{
       string IntInTemp=IntIn+j.ToString()+"|";
       //IntIn+=j.ToString()+"|";
       string StrInTemp=StrIn+mid;
       //StrIn+=mid;
       Combination(star,IntInTemp,StrInTemp,l);
       //}
      }
     }
     
      
    }
    #endregion
   }
  }

  
 }
}

posted on 2006-07-31 19:21  Yang-S  阅读(354)  评论(0)    收藏  举报