zoukankan      html  css  js  c++  java
  • 组合 z

    输入a b c d e以及它们对应的数字
    比如
    a-->1 2 3 
    b-->2 3
    c-->1
    d-->3 4 5
    e-->1 3 5
    输出a b c d e的可用组合,a b c d e不重复,如
    a-->2
    b-->3
    c-->1
    d-->4
    e-->5

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var dict = new Dictionary<char, List<int>>()
                {
                    { 'a', new List<int> { 1, 2, 3 } },
                    { 'b', new List<int> { 2, 3 } },
                    { 'c', new List<int> { 1 } },
                    { 'd', new List<int> { 3, 4, 5 } },
                    { 'e', new List<int> { 1, 3, 5 } }
                };
                foo(dict, new Dictionary<char, int>());
            }
     
            static void foo(Dictionary<char, List<int>> data, Dictionary<char, int> pre)
            {
                if (data.Count == 0)
                {
                    Console.WriteLine("found:");
                    foreach (var item in pre.OrderBy(x => x.Key))
                        Console.WriteLine("{0} - {1}", item.Key, item.Value);
                    return;
                }
                var first = data.OrderBy(x => x.Value.Count()).First();
                foreach (var item in first.Value)
                {
                    foo(data.OrderBy(x => x.Value.Count()).Skip(1).ToDictionary(x => x.Key, x => x.Value.Except(new int[] { item }).ToList()), pre.Cast<KeyValuePair<char, int>>().Concat(new KeyValuePair<char, int>[] { new KeyValuePair<char, int>(first.Key, item) }).ToDictionary(x => x.Key, x => x.Value));
                }
            }
        }
    }
    
  • 相关阅读:
    smb上传图片工具类
    hzero
    ORACLE
    数据库范式
    数据库设计阶段
    Java变量和运算符
    相对路径和绝对路径
    setTimeout()方法和setInterval()方法
    body onload()事件和table insertRow()、tr insertCell()
    eval函数和isNaN函数
  • 原文地址:https://www.cnblogs.com/zeroone/p/4199138.html
Copyright © 2011-2022 走看看