找出字符串中所有能被3整除的数字

字符串中是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; }
    }
}

 

posted on 2021-04-15 19:31  荆棘人  阅读(649)  评论(0编辑  收藏  举报

导航