字符串中是0-9隨機出現的數字。
最先想到的方案一般是循環然后取余,但是如果仔細分析的話,就會發現當數字很大這種方案不可行。
數學定理如果一個數字各個位上的數字和能被3整除這個數字就能被3整除,那么考慮通過計算各個位上的數字和方案就會變的可行;
然后再進行優化,可以提前對每個位上的數字對3取余,其實這一步並不是必須的。
圖片展示:
示例代碼:
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test3BeiShu { class Program { static void Main(string[] args) { //List<string> list = new List<string>(10000); //Random random = new Random(int.Parse(DateTime.Now.ToString("ddHHmmss"))); //int totalNum = 0; //while (true) //{ // int num = random.Next(1, int.MaxValue); // if (!list.Contains(num.ToString())) // { // list.Add(num.ToString()); // totalNum += 1; // } // if (totalNum >= 10000) // { // break; // } //} //Stopwatch watch = new Stopwatch(); //watch.Start(); //int num3BeiShu = 0; //foreach (var num in list) //{ // int numtemp = int.Parse(num); // if (numtemp % 3 == 0) // { // num3BeiShu++; // } //} //watch.Stop(); //Console.WriteLine(num3BeiShu); //Console.WriteLine("運行時長:"+watch.ElapsedMilliseconds); //long bigNum = 6545645614845454545L; //Stopwatch watch1 = new Stopwatch(); //watch1.Start(); //bool r = bigNum % 3==0; //watch1.Stop(); //long time = watch1.ElapsedMilliseconds; //Console.WriteLine("運行時間:"+time); string numStr = "454654974321564379787316235974551362145649789794654465"; //string numStr = "12345"; List <NumPoint> list = new List<NumPoint>(); for (int i = 0; i < numStr.Length; i++) { NumPoint tempNumPoint = new NumPoint(); tempNumPoint.index = i; int pointVal = int.Parse(numStr[i].ToString()); int point3QuYuVal = pointVal % 3; tempNumPoint.Is3BeiShu = point3QuYuVal == 0; tempNumPoint.Num = pointVal; tempNumPoint.point3QuYuValue = point3QuYuVal; list.Add(tempNumPoint); } List<string> allNum = new List<string>(); int index = 0; while (index<list.Count) { NumPoint startPoint= list[index]; if (startPoint.Is3BeiShu) { if (!allNum.Contains(startPoint.Num.ToString())) { allNum.Add(startPoint.Num.ToString()); } } int lastIndex = list.Count - 1; int moveIndex = index; while (moveIndex < lastIndex ) { moveIndex += 1;//移動一位 NumPoint newPoint= list[moveIndex]; int startIndex = startPoint.index; int endIndex = newPoint.index; int totalQuYuVal = 0; for (int i = startIndex; i <= endIndex; i++) { totalQuYuVal += list[i].point3QuYuValue; } if (totalQuYuVal % 3 == 0) { StringBuilder sb = new StringBuilder(); for (int i = startIndex; i <= endIndex; i++) { sb.Append(list[i].Num); } string zuHeNumStr = sb.ToString(); if (!allNum.Contains(zuHeNumStr)) { allNum.Add(zuHeNumStr); } } } index += 1; } int totalNum = allNum.Count(); Console.WriteLine("總的數量:"+totalNum); foreach (var item in allNum) { Console.WriteLine(item); } Console.ReadKey(); } } /// <summary> /// 每個位上的數字對象 /// </summary> public class NumPoint { /// <summary> /// 數字所在索引 /// </summary> public int index { get; set; } /// <summary> /// 位置上的數對3取余的值 /// </summary> public int point3QuYuValue { get; set; } /// <summary> /// 位置上的數字值 /// </summary> public int Num { get; set; } /// <summary> /// 是否是三的倍數 /// </summary> public bool Is3BeiShu { get; set; } } }