我想找到2个3位数字的最大产品,它是一个回文,我的算法即使它应该也没有完成

Deu*_*ral 0 c#

我在C#中编写了以下代码.我已经从各个方面看了它,我认为它应该可以工作但是即使我让它在一夜之间运行也没有给出任何输出.有什么问题?提前致谢.

namespace Palymdrome
{
    class Multiple
    {
        private int x;
        private int y;

        public int ProductY
        {
            get
            {
                return y;
            }

            set
            {
                y = value;
            }


        }
        public int ProductX
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }
        public int Value
        {
            get
            {
                return x * y;
            }
        }

        public Multiple(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }


    class Program
    {
        static bool IsPalimdrome(int palimdrome)
        {
            string sPalimdrome = palimdrome.ToString();
            int decrescent = sPalimdrome.Length-1;
            string sInverted = "";
            for (decrescent=sPalimdrome.Length-1;decrescent>=0;decrescent--)
            {
                sInverted += sPalimdrome[decrescent];
            }

            if (sPalimdrome == sInverted)
            {
                return true;
            }

            return false;
        }

        static void Main(string[] args)
        {
            Multiple multiple = new Multiple(999, 999);
            int[] values = new int[999999];
            int i = 0;

            while (multiple.ProductY > 0)
            {
                multiple.ProductX--;
                if (multiple.ProductX == 0)
                {
                    multiple.ProductY--;
                    multiple.ProductX = 999;
                }

                if (IsPalimdrome(multiple.Value) && multiple.Value != 0)
                {
                    values[i] = multiple.Value;
                    i++;
                }

                /*if (multiple.ProductY < 10)
                {
                    Console.WriteLine("100 reached, waiting for confirmation");
                    Console.Read();
                }*/



            }

            Console.WriteLine(values.Max());
            Console.Read();


        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*Jon 5

我没有完全遵循代码来确切地知道它为什么不起作用,但是男人哦,男人是你让事情变得复杂.

首先,IsPalindrome使用LINQ 实现是微不足道的:

static bool IsPalindrome(int number)
{
    var s = number.ToString();
    return s.Reverse().SequenceEqual(s);
}
Run Code Online (Sandbox Code Playgroud)

此外,循环的时候你肯定不会需要考虑到的帐户值xy小于100,因为该产品将不会被定义为两个3位数字的产品.此外,您不需要检查两者x * yy * x任何固定值x,y因为产品将再次相同.这意味着对于任何一个值,x您可以忽略所有y小于的值x.

最后,您不需要分配具有999999个元素的数组.A List<int>会做得很好.

所以,你可以简单地做:

var results = new List<int>();

for(var x = 100; x < 1000; ++x) {
    for(var y = x; y < 1000; ++y) {
        if (IsPalindrome(x * y)) {
            results.Add(x*y);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我们全力以赴为LINQ:

var result = 
    Enumerable.Range(100, 900).
    SelectMany(x => Enumerable.Range(x, 1000 - x).Select(y => x * y)).
    Where(IsPalindrome).
    Max();
Run Code Online (Sandbox Code Playgroud)