欧拉计划 第4题

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99.

Find the largest palindrome made from the product of two 3-digit numbers.

 

class Program
    {
        static void Main(string[] args)
        {
            /*
             * A palindromic number reads the same both ways. 
             * The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99.
             * Find the largest palindrome made from the product of two 3-digit numbers.
             */

            Console.WriteLine(CheckPalindromicNumber(123));
            Console.WriteLine(CheckPalindromicNumber(121));
            Console.WriteLine(CheckPalindromicNumber(1221));
            Console.WriteLine(CheckPalindromicNumber(12321));

            int k = 1;
            for (int i = 999; i >= 0; i--)
            {
                for (int j = 999; j >= 0; j--)
                {
                    int n = i*j;
                    if (n > k && CheckPalindromicNumber(n))
                    {
                        k = n;
                    }
                }
            }
            Console.WriteLine(k);
        }

        private static bool CheckPalindromicNumber(int n)
        {
            string nString = n.ToString();
            for (int a = 0, b = nString.Length - 1; a < b; a++, b--)
            {
                if (nString[a] != nString[b]) return false;
            }
            return true;
        }

    }

 

posted @ 2011-08-28 17:24  kiminozo  阅读(338)  评论(0编辑  收藏  举报