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);

            }
        }
    }

  • 相关阅读:
    [FAQ] Cannot use object of type MongoInt64 as array
    [Go] 选择 Beego 的三个理由
    [PHP] 有关PHP浮点数默认显示位数 precision 以及如何调整
    [FAQ] Beego2.0.2 bee 生成的 api 项目运行 404, http server Running on http://:8080
    [K8s] Kubernetes核心基础概念 Node, Pod, ReplicaSet, Deployment, Service, Ingress, ConfigMap
    [FE] uViewUI u-navbar 曲线解决 uni onNavigationBarButtonTap 的限制与失效
    Xmind 8 pro 破解版详细安装教程
    SQL 触发器 暂停 和 启动
    SQL 代理服务无法启动
    MongoDB 项目实例-用户信息增删改查
  • 原文地址:https://www.cnblogs.com/facingwaller/p/2388579.html
Copyright © 2011-2022 走看看