天天看點

回文數

/// <summary>
        /// 判斷某個數的平方是否是回文數
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            for (int i = 0; i < 256; i++)
            {
                Program p = new Program();
                int temp = i * i;
                if (temp == p.ReverseInt(i))
                {
                    Console.WriteLine(i);
                }
            }
        }
        /// <summary>
        /// 将一個整數反序。如:123反序後為321
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public int ReverseInt(int x)
        {
            int temp = 0;
            while (true)
            {
                if (x==0)
                {
                    break;
                }
                temp = temp * 10 + x % 10;
                x /= 10;
            }
            return temp;
        }