博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编程-统计并输出符合条件的字串组合
阅读量:6882 次
发布时间:2019-06-27

本文共 5094 字,大约阅读时间需要 16 分钟。

背景

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

问题描述

  有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    {        ///         /// 统计可选数字最小值        ///         private static readonly int MinNum = 0;        ///         /// 统计可选数字最大值        ///         private static readonly int MaxNum = 9;        ///         /// 组合字串中某个数字的最小重复个数        ///         private static readonly int MinRepeatCount = 1;        ///         /// 组合字串中某个数字的最大重复个数        ///         private static readonly int MaxRepeatCount = 5;        ///         /// 符合条件的所有统计结果        ///         static readonly List
AllStatisticsResult = new List
(); /* * 符合条件,且所有重复次数的统计结果 * * 数据结构:(可选数字,重复次数,组合字串) * 例如: * [ * (0, * (1, [01117, 01126, ...]), * (2, [00118, 00127, ...]), * ..... * ) * ] */ private static readonly Dictionary
>> SpecialStatisticsResult = new Dictionary
>>(10); ///
/// 数字组合格式 /// private static string sortFormat = "{0}{1}{2}{3}{4}"; ///
/// 输出路径 /// 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); } ///
/// 初始化数据存储结构 /// private static void Initialize() { for (int i = MinNum; i <= MaxNum; i++) { var numRepeatCountDic = new Dictionary
>(); for (int j = MinRepeatCount; j <= MaxRepeatCount; j++) { numRepeatCountDic.Add(j, new List
()); } SpecialStatisticsResult.Add(i, numRepeatCountDic); } } ///
/// 计算并保存当前组合字串中不同数字的重复个数 /// ///
组合子串 ///
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); } } } } ///
/// 获取统计结果,并以字符串方式输出 /// ///
统计结果
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(); } ///
/// 将统计结果保存到文件中 /// ///
统计结果 private static void Output(string statisticsResult) { using (StreamWriter writer = new StreamWriter(OutputPath)) { writer.WriteLine(statisticsResult); writer.Flush(); } } }

统计结果 

 

转载于:https://www.cnblogs.com/arvinzhang/p/8413712.html

你可能感兴趣的文章
承接上面一遍的(后续步骤)
查看>>
杂笔感想
查看>>
Source Insight 常用设置
查看>>
android anr什么意思?
查看>>
视频: DroidPilot - 增强多应用集成测试 - V2.1.0 (新)
查看>>
python实现逐行替换超大文件中的字符串
查看>>
软件工程例子
查看>>
python全栈开发,Day43(引子,协程介绍,Greenlet模块,Gevent模块,Gevent之同步与异步)...
查看>>
异步编程与多线程编程的联系和区别
查看>>
单例模式(Singleton Pattern)
查看>>
SQL Server
查看>>
pre标签内文本自动换行
查看>>
RCF的简单使用教程以及什么是回调函数
查看>>
java的Pattern类
查看>>
2019.2.18 区块链论文翻译
查看>>
HDU-统计难题
查看>>
python的PIL模块安装
查看>>
数据结构与算法(0)-四则运算
查看>>
ASP.NET-FineUI开发实践-17
查看>>
BETA 版冲刺前准备
查看>>