zoukankan      html  css  js  c++  java
  • 编程-统计并输出符合条件的字串组合

    背景

      有一天晚上还在加班时,一个朋友打电话让帮忙统计下几个数字符合某种条件的所有可能结果,描述了好大一会才明白。编程不麻烦,抽空一会就写好了,这里做个简单的记录。

    问题描述

      有5个变量,分别为a、b、c、d、e, 取值范围都是0~9。 5个变量相加,个位数字为0。按照以下条件统计:

    • 统计所有符合条件的结果
    • 统计0~9个数字分别出现1~5次的结果

    分析

      记录所有符合条件的结果比较简单,使用一个集合即可。但是记录每个数字分别出现不同次数的结果有点复杂。经过分析,应该有3层结构:

    1. 记录所有数字
    2. 记录所有数字重复的次数
    3. 记录所有数字不同重复次数对应的组合串

    最终确定使用一下数据结构:Dictionary<int, Dictionary<int, List<string>>>

    编程

    处理步骤

    1. 初始化存储数据结构
    2. 记录所有符合条件的统计结果
    3. 记录每个数字不同重复次数的统计结果,使用正则匹配个数
    4. 以一定格式输出到文件中

    代码

    public class Stastistics
        {
            /// <summary>
            /// 统计可选数字最小值
            /// </summary>
            private static readonly int MinNum = 0;
    
            /// <summary>
            /// 统计可选数字最大值
            /// </summary>
            private static readonly int MaxNum = 9;
    
            /// <summary>
            /// 组合字串中某个数字的最小重复个数
            /// </summary>
            private static readonly int MinRepeatCount = 1;
    
            /// <summary>
            /// 组合字串中某个数字的最大重复个数
            /// </summary>
            private static readonly int MaxRepeatCount = 5;
    
            /// <summary>
            /// 符合条件的所有统计结果
            /// </summary>
            static readonly List<string> AllStatisticsResult = new List<string>();
    
            /*
             * 符合条件,且所有重复次数的统计结果
             * 
             * 数据结构:(可选数字,重复次数,组合字串)
             * 例如:
             * [
             *  (0,
             *      (1, [01117, 01126, ...]),
             *      (2, [00118, 00127, ...]),
             *      .....
             *   )
             * ]
             */
            private static readonly Dictionary<int, Dictionary<int, List<string>>> SpecialStatisticsResult = new Dictionary<int, Dictionary<int, List<string>>>(10);
    
            /// <summary>
            /// 数字组合格式
            /// </summary>
            private static string sortFormat = "{0}{1}{2}{3}{4}";
    
            /// <summary>
            /// 输出路径
            /// </summary>
            private static string OutputPath = @"D:StatisticsResult.txt";
    
            public static void Run()
            {
                Initialize();
    
                // 统计并记录所有符合条件的结果
                for (int i = MinNum; i <= MaxNum; i++)
                {
                    for (int j = MinNum; j <= MaxNum; j++)
                    {
                        for (int k = MinNum; k <= MaxNum; k++)
                        {
                            for (int l = MinNum; l <= MaxNum; l++)
                            {
                                for (int m = MinNum; m <= MaxNum; m++)
                                {
                                    var total = i + j + k + l + m;
                                    if (total % 10 == 0)
                                    {
                                        AllStatisticsResult.Add(string.Format(sortFormat, i, j, k, l, m));
                                    }
                                }
                            }
                        }
                    }
                }
    
                // 统计符合条件,不同重复次数的结果
                foreach (var numCombs in AllStatisticsResult)
                {
                    CalculateSpecial(numCombs);
                }
    
                //获取所有分类统计结果的字符串格式
                string statisticsResult = GetStastisticsResult();
    
                //将统计结果输出并保存到文件中
                Output(statisticsResult);
            }
    
            /// <summary>
            /// 初始化数据存储结构
            /// </summary>
            private static void Initialize()
            {
                for (int i = MinNum; i <= MaxNum; i++)
                {
                    var numRepeatCountDic = new Dictionary<int, List<string>>();
                    for (int j = MinRepeatCount; j <= MaxRepeatCount; j++)
                    {
                        numRepeatCountDic.Add(j, new List<string>());
                    }
    
                    SpecialStatisticsResult.Add(i, numRepeatCountDic);
                }
            }
    
            /// <summary>
            /// 计算并保存当前组合字串中不同数字的重复个数
            /// </summary>
            /// <param name="numComb">组合子串</param>
            /// <returns></returns>
            private static void CalculateSpecial(string numComb)
            {
                for (int i = MinNum; i <= MaxNum; i++)
                {
                    //当前数字对应的字典
                    var currentNumRepeatCountDic = SpecialStatisticsResult.FirstOrDefault(s => s.Key == i);
    
                    for (int j = MinRepeatCount; j <= MaxRepeatCount; j++)
                    {
                        //当前重复个数对应的字典
                        var currentRepeatCountDic = currentNumRepeatCountDic.Value.FirstOrDefault(s => s.Key == j);
                        if (Regex.Matches(numComb, i.ToString()).Count == j)
                        {
                            currentRepeatCountDic.Value.Add(numComb);
                        }
                    }
                }
            }
    
            /// <summary>
            /// 获取统计结果,并以字符串方式输出
            /// </summary>
            /// <returns>统计结果</returns>
            private static string GetStastisticsResult()
            {
                StringBuilder statisticsResultBuilder = new StringBuilder();
    
                statisticsResultBuilder.AppendFormat("--------所有样本总数:{0}--------", AllStatisticsResult.Count);
                foreach (var s in AllStatisticsResult)
                {
                    statisticsResultBuilder.AppendFormat("{0},", s);
                }
    
                statisticsResultBuilder.AppendLine().AppendLine();
    
                for (int i = MinNum; i <= MaxNum; i++)
                {
                    var currentValue = SpecialStatisticsResult.FirstOrDefault(s => s.Key == i);
                    if (currentValue.Value.Count <= 0)
                    {
                        continue;
                    }
    
                    for (int j = MinRepeatCount; j <= MaxRepeatCount; j++)
                    {
                        var currentNumComb = currentValue.Value.FirstOrDefault(s => s.Key == j);
                        if (currentNumComb.Value.Count <= 0)
                        {
                            continue;
                        }
    
                        statisticsResultBuilder.AppendFormat("--------{0} 重复 {1} 次的总数:{2}--------", i, j, currentNumComb.Value.Count);
    
                        foreach (var s in currentNumComb.Value)
                        {
                            statisticsResultBuilder.Append(s + ", ");
                        }
    
                        statisticsResultBuilder.AppendLine().AppendLine();
                    }
                }
    
                return statisticsResultBuilder.ToString();
            }
    
            /// <summary>
            /// 将统计结果保存到文件中
            /// </summary>
            /// <param name="statisticsResult">统计结果</param>
            private static void Output(string statisticsResult)
            {
                using (StreamWriter writer = new StreamWriter(OutputPath))
                {
                    writer.WriteLine(statisticsResult);
                    writer.Flush();
                }
            }
        }

    统计结果 

  • 相关阅读:
    今天一个人跑了趟香山
    周六钻胡同
    (转)C#中protected用法详解
    C# base和this
    error BK1506 : cannot open file '.\Debug\ex73View.sbr': No such file or directory
    error PRJ0003 : Error spawning 'cmd.exe'
    VS2008卸载时遇到“加载安装组件时遇到问题,取消安装” 在卸载或者升级VS2008的时候,遇到“加载安装组件时遇到问题,取消安装”的情况
    我们在建立Win32工程的时候,要选择是Win32控制台应用程序还是Win32项目,那么两者到底有什么区别呢?
    开发板重新烧写时出现ERROR: Checksum failure (expected=0x3D67E6F computed=0x3E0E0CA)
    把PC上的代码移植到WINCE上
  • 原文地址:https://www.cnblogs.com/arvinzhang/p/8413712.html
Copyright © 2011-2022 走看看