編程-統計並輸出符合條件的字串組合
阿新 • • 發佈:2018-02-04
步驟 sin 簡單的 不同 returns tput def ima 獲取
背景
有一天晚上還在加班時,一個朋友打電話讓幫忙統計下幾個數字符合某種條件的所有可能結果,描述了好大一會才明白。編程不麻煩,抽空一會就寫好了,這裏做個簡單的記錄。
問題描述
有5個變量,分別為a、b、c、d、e, 取值範圍都是0~9。 5個變量相加,個位數字為0。按照以下條件統計:
- 統計所有符合條件的結果
- 統計0~9個數字分別出現1~5次的結果
分析
記錄所有符合條件的結果比較簡單,使用一個集合即可。但是記錄每個數字分別出現不同次數的結果有點復雜。經過分析,應該有3層結構:
- 記錄所有數字
- 記錄所有數字重復的次數
- 記錄所有數字不同重復次數對應的組合串
最終確定使用一下數據結構:Dictionary<int, Dictionary<int, List<string>>>
編程
處理步驟
- 初始化存儲數據結構
- 記錄所有符合條件的統計結果
- 記錄每個數字不同重復次數的統計結果,使用正則匹配個數
- 以一定格式輸出到文件中
代碼
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(); } } }
統計結果
編程-統計並輸出符合條件的字串組合