日期:2014-05-18  浏览次数:20762 次

请问怎么样把下面的代码改成递归的?
题目要求是输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻 
这是我改别人的代码,现在怎么用递归去实现,貌似递归代码只有10多行而已,写不出来。。。。
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{

  public  class MySort
    {

      private List<string> s0 = new List<string>();
      private List<string> r0 = new List<string>();
      public MySort()
      {
          s0.Add("1");
          s0.Add("2");
          s0.Add("2");
          s0.Add("3");
          s0.Add("4");
          s0.Add("5");
      }
      private void Sort()
      {
          string str="";
          for(int i0=0;i0<s0.Count;i0++){
              List<string> s1=getNextList(s0,i0);
              for(int i1=0;i1<s1.Count;i1++){
              List<string> s2=getNextList(s1,i1);
                   for(int i2=0;i2<s2.Count;i2++){
              List<string> s3=getNextList(s2,i2);
                        for(int i3=0;i3<s3.Count;i3++){
              List<string> s4=getNextList(s3,i3);
                             for(int i4=0;i4<s4.Count;i4++){
              List<string> s5=getNextList(s4,i4);
                                  for(int i5=0;i5<s5.Count;i5++){
             str=s0[i0]+s1[i1]+s2[i2]+s3[i3]+s4[i4]+s5[i5];
                                       if (check(str)&&!r0.Contains(str)) {
                                    r0.Add(str);
                                }
                                  }
                             }
                        }
                   }
              }
          }
      }
      private List<String> getNextList(List<String> inList, int i) {
        List<String> rs = new List<String>();
        rs.AddRange(inList);
        rs.Remove(rs[i]);
        return rs;
    }

      private bool check(String s) {
 
        
        if (s[2] == '4' || s.Contains("35") || s.Contains("53")) {
            return false;
        }
        return true;
    }
        private void printStr() {
        int lineN = 10;
    

        foreach(var s in r0) {
            Console.Write(s + " ");
            if (lineN-- == 1) {
                 Console.WriteLine();
                lineN = 10;
            }
        } 
            Console.WriteLine(r0.Count);
    }

        static void Main(string[] args)
        {
            MySort mySort = new MySort();
            mySort.Sort();
            mySort.printStr();
            Console.Read();
        }
    }
 }
  




------解决方案--------------------
非递归比递归运行效率更高,占用资源更少!
------解决方案--------------------
楼主目的是什么,好长哦
------解决方案--------------------
输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻 

排列的个数有限制吗?1个到6个数字排在一起都算吗?例如,一个1,算是一个组合吗?
------解决方案--------------------
知道了,我看看先 - -
------解决方案--------------------
比较复杂,给个思路看看行不行:

1,按顺序从原始List抽取一个数。
2,使用这个数和抽取剩下的数据搭配得到一个组合(循环放到 0 to 5的位置 )。
3,如果组合还未存在于结果集而且不符合给定的条件(4不能在第三位,3和5不能相邻 ),则存到结果集。
4,如果原始List.Count>0,则递归调用自己。


------解决方案--------------------
http://topic.csdn.net/u/20100609/12/d4f01c1c-3173-4a7f-b670-76cd34dd2796.html