gpt4 book ai didi

javascript - 我对两个三位数乘积的最大回文数的解决方案需要工作

转载 作者:太空宇宙 更新时间:2023-11-04 16:31:24 25 4
gpt4 key购买 nike

我得到的答案不是正确的(正确答案是 906609)。请帮助我理解我哪里出了问题。我希望 while 循环从 100 到 999,同时在循环递增之前将其自身与当前 i 值相乘。

// 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.

var pali = [];

function palindrome() {
for (var i = 100; i <= 999; i++) {
var counter = 100;

while (counter <= 999) {
var result = counter * i;
if (result.toString() === result.toString().split("").reverse().join("")) {
pali.push(result);
}
counter++;
}
}
return pali[pali.length - 1];
}

console.log(palindrome());

最佳答案

如果您希望最后一个成为最高的,则必须按升序对数组进行排序:

pali.sort(function(a, b){return a-b});

使用它,我得到 906609。

关于javascript - 我对两个三位数乘积的最大回文数的解决方案需要工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39776410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com