简体   繁体   English

最大的回文产品-C#

[英]Largest palindrome product - C#

Hello fellow programmers 各位程序员大家好

I'm currently trying to solve some problems on Project Euler in C# to improve my knowledge. 我目前正在尝试解决C#中的Euler项目上的一些问题,以提高我的知识。 However, one solution I made for Problem #4 isn't working even though it should. 但是,我为问题4提出的一种解决方案即使可行也无法正常工作。

A palindromic number reads the same both ways. 回文数在两个方向上都相同。 The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. 由两个两位数的乘积构成的最大回文数为9009 = 91×99。

Find the largest palindrome made from the product of two 3-digit numbers. 查找由两个3位数字的乘积组成的最大回文。

Any ideas? 有任何想法吗?

    namespace ProjectEuler_4
{
    class Program
    {
        static void Main(string[] args)
        {

            for (int i = 999; i >= 100; i--)
            {
                for (int j = 999; j >= 100; j--)
                {
                    if (isPalindome(i *j) == true)
                    {
                        Console.WriteLine("Digit1: " + i);
                        Console.WriteLine("Digit2: " + j);
                        Console.WriteLine("Outcome: " + i * j);
                          Console.ReadLine();
                    }
                    else
                    {

                        Console.WriteLine(i + " " + j + " =nope");

                    }
                }


            }
            Console.ReadLine();
        }
        public static bool isPalindome(int num)
        {
            string sNum = num.ToString();
            for (int i = 0; i < sNum.Length / 2; i++)
                if (sNum[i] != sNum[sNum.Length - 1 - i]) return false;

            return true;
        }

    }
}

Outcome is : 结果是:

Digit1: 995 Digit2: 583 Total: 580085 数字1:995数字2:583总计:580085

Though, that is not the right answer. 虽然,这不是正确的答案。 Anything I did wrong? 我做错了什么? I'm not asking for a finished solution, just want to understand what the problem is with this one. 我不是要一个完整的解决方案,只是想了解这个问题是什么。

Try checking all combinations. 尝试检查所有组合。 For example, 994 * 994 is larger, and you didn't even check it. 例如,994 * 994较大,您甚至都没有检查它。

Your program actually works and finds the right answer. 您的程序实际上可以正常工作并找到正确的答案。 However, it doesn't print the biggest number first, it's the third output instead. 但是,它不会先输出最大的数字,而是输出第三个输出。

For example, i=999 j=2 comes before i=998 j=998, but the second product is much bigger. 例如,i = 999 j = 2早于i = 998 j = 998,但是第二个乘积要大得多。 See, you don't find the products in descending order. 看,您找不到降序排列的产品。

The good solution is a simple max-finding loop (kinda-pseudo-code): 好的解决方案是一个简单的最大查找循环(​​kinda-pseudo-code):

var best = -1;
for (i, j) {
  if (isPalindrome(i*j) && i*j > best) {
    best = i*j;
  }
}
Console.WriteLine(best);

I have a few notes: 我有几点注意事项:

  • It's better to start the inner loop from i . 最好从i开始进行内部循环。
  • We are able to leave the inner loop if a palindrome has been found, because it's guaranteed that for the current i there is no numbers greater than number . 如果找到回文,我们可以离开内循环,因为可以保证对于当前i ,没有大于number
  • It makes sense to search for the multiplicands that gives the biggest product in the range 99..9 - 90..0 , because they are definitely exist in this range. 搜索在99..9 - 90..0范围内给出最大乘积的99..9 - 90..0 ,因为它们肯定存在于该范围内。
  • The fastest condition should be the first in the if statement. 最快的条件应该是if语句中的第一个条件。
  • I've used a long type for variables to make the code below work for bigger numbers. 我使用了一个long型变量,以使下面的代码适用于更大的数字。

Try this code sample: 尝试以下代码示例:

// Store the maximum palindrome number here:
long maxNumber = 0;

// The maximum multiplicand (typically: 9...9):
const int NMax = 999;
// The minimum multiplicand.
// Obviously, it couldn't be less than 90...0:
const int NMin = NMax - (NMax + 1) / 10 + 1;

for (int i = NMax; i > NMin; i--)
{
    // Starting from i since i * j = j * i for any i, j:
    for (int j = i; j > NMin; j--)
    {
        long number = Math.BigMul(i, j);
        // The fastest condition should be the first in the `if` statement:
        if (number > maxNumber && isPalindome(number))
        {
            maxNumber = number;
            Console.WriteLine("{0} = {1} * {2}", number, i, j);
            break; // Leave the `j` loop, because it's guaranteed that there is
                   // no numbers greater than `number` for the current `i`
        }
    }
}

Output: 输出:

for NMax=999:     906609 = 993 * 913
for NMax=9999:    99000099 = 9999 * 9901
for NMax=9999999: 99956644665999 = 9998017 * 9997647

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM