quark

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Problem Description

It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.

Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.

 

C#

This problem is simple, we just try each number one by one.

        // this is just a guess.
        private const int MIN_VALUE = 10000;

        public static void Run()
        {
            int num = MIN_VALUE;

            while (true)
            {
                var charArray = num.ToString().ToCharArray();
                // if the higest digit of this num is not 1, then we add num by one digit
                if (charArray[0] != '1')
                {
                    num = (int)Math.Pow(10, charArray.Length) + 1;
                }

                // this is an anonymous function to get the sorted digit string of num
                Func<int, string> GetDigitStr = delegate(int n)
                {
                    return string.Concat(n.ToString().ToCharArray().OrderBy(c => c));
                };

                var digitStr = GetDigitStr(num);

                bool isOK = true;
                for (int i = 2; i <= 6; i++)
                {
                    int temp = num * i;

                    var tempStr = GetDigitStr(temp);
                    if (tempStr != digitStr)
                    {
                        isOK = false;
                        break;
                    }
                }

                if (isOK)
                {
                    Console.WriteLine("result = {0}", num);
                    break;
                }
                num++;
            }
 
        }
posted on 2012-06-21 10:56  QuarkZ  阅读(170)  评论(0编辑  收藏  举报