zoukankan      html  css  js  c++  java
  • [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++;
                }
     
            }
  • 相关阅读:
    2021广东省强网杯WriteUp
    2021 数字四川创新大赛WriteUp
    2021 陇剑杯wp
    2021 羊城杯WriteUP
    如何翻安全四大顶会的文章
    2021 祥云杯 wp
    codeql初探
    sqlmap应用
    sql注入2
    sql注入
  • 原文地址:https://www.cnblogs.com/quark/p/2557402.html
Copyright © 2011-2022 走看看