输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻

16 篇文章 0 订阅
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();
        }
    }
 }
  


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值