zoukankan      html  css  js  c++  java
  • 【欧拉题四】三位数乘积所得的最大回文数


    题目

    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.

    代码 

     using System;

    using System.Collections.Generic;

    /*
     * 
     * 从 X1=999 ,X2=999
     * 判断乘积是否回文转换成字符串, 
     * 依次减 X1和X2  
     * 
     * 
    */
    namespace OulerProblem
    {
        public class Problem4
        {
             
            public static string  q4 ()
            {
                List<int> list=new List<int> ();
                int x1 = 999, x2 = 999;
                bool isok = false;
                for (int k = x1 ; k >= 1; k--) {                
                    for (int j = x2 ; j >= 1; j--) {
                        
                        string palNum = (k * j).ToString ();
                         
                        isok = isStringP (palNum);
                        if (isok) {
                            list.Add(k*j);
                             
                        }     
                    }
                         
                    
                }
                     
                    list.Sort();
                
                return ""+list[list.Count-1] ;    
            }

            public static  bool isStringP (string palNum)
            {
                bool isok = false;
                var array1 = palNum.ToCharArray ();
                int last = array1.Length - 1;
                int i = 0;
             
                while (true) {//单个字符相等                
                    if (array1 [i] != array1 [last - i]) {                     
                        break;
                    }  
                    if (i == array1.Length - i || i == array1.Length - i - 1) {                         
                        isok = true;
                        break;
                    }                
                    i++;
                }
                return isok;
            }

            public static void test ()
            {
                //    Console.WriteLine (isStringP ((99 * 91).ToString ()));
                
                var t1 = DateTime.Now;
                Console.WriteLine (" " + q4 () + " cast time " + (DateTime.Now - t1).TotalSeconds);

            }
        }
    }

  • 相关阅读:
    Python随笔之字典Dict
    Python随笔之列表List
    Python基础整理,懒得分类了,大家对付看看吧
    shell批量推送公钥脚本
    Xcode 切换target 之后打包失败
    百度导航sdk错误日志
    前端项目中去掉替换双引号
    taro3.x项目中引用taro-ui以及taro-iconfont-cli
    taro
    JS中some() every() forEach() map() filter()的区别
  • 原文地址:https://www.cnblogs.com/facingwaller/p/2388579.html
Copyright © 2011-2022 走看看