• [Project Euler] Problem 52


    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++;
                }
     
            }
  • 相关阅读:
    java对象和json对象之间互相转换
    SVN使用教程总结
    JVM调优总结-调优方法
    tomcat 高并发配置 与优化
    redis的主从复制配置
    Java实现Redis持久化到数据库的关键方法
    java操作redis
    SSO系统的实现
    jedisClient操作redis实现增删改查功能
    get与post
  • 原文地址:https://www.cnblogs.com/quark/p/2557402.html
Copyright © 2020-2023  润新知