有一天晚上還在加班時,一個朋友打電話讓幫忙統計下幾個數字符合某種條件的全部可能結果,描述了好大一會才明白。編程不麻煩,抽空一會就寫好了,這裏作個簡單的記錄。編程
有5個變量,分別爲a、b、c、d、e, 取值範圍都是0~9。 5個變量相加,個位數字爲0。按照如下條件統計:數據結構
記錄全部符合條件的結果比較簡單,使用一個集合便可。可是記錄每一個數字分別出現不一樣次數的結果有點複雜。通過分析,應該有3層結構:ui
最終肯定使用一下數據結構:Dictionary<int, Dictionary<int, List<string>>>orm
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(); } } }