javascript - 欧拉项目#8——Javascript

标签 javascript

/***
 * Problem 8 -- Largest Product in a Series
 *
 * @author RepeaterCreeper
 */

var grid = `73167176531330624919225119674426574742355349194934
    96983520312774506326239578318016984801869478851843
    85861560789112949495459501737958331952853208805511
    12540698747158523863050715693290963295227443043557
    66896648950445244523161731856403098711121722383113
    62229893423380308135336276614282806444486645238749
    30358907296290491560440772390713810515859307960866
    70172427121883998797908792274921901699720888093776
    65727333001053367881220235421809751254540594752243
    52584907711670556013604839586446706324415722155397
    53697817977846174064955149290862569321978468622482
    83972241375657056057490261407972968652414535100474
    82166370484403199890008895243450658541227588666881
    16427171479924442928230863465674813919123162824586
    17866458359124566529476545682848912883142607690042
    24219022671055626321111109370544217506941658960408
    07198403850962455444362981230987879927244284909188
    84580156166097919133875499200524063689912560717606
    05886116467109405077541002256983155200055935729725
    71636269561882670428252483600823257530420752963450`;

/**
 * Largest Adjacent Numbers
 * 
 * @returns int
 */
function largestAdjacentNumbers(grid, consecutiveLength = 4, separator = "\n") {
  let gridData = grid.split("\n"),
    largestProduct = 0;

  for (var row in gridData) {
    currentRow = gridData[row].split('').map(x => parseInt(x));

    for (var i = 0; i < currentRow.length - consecutiveLength; i++) {
      combination = currentRow.slice(i, i + consecutiveLength);
      if (!combination.includes(0)) {
        product = combination.reduce(function(a, b) {
          return a * b
        });
        if (largestProduct < product) largestProduct = product;
      }
    }
  }

  return largestProduct;
}

console.log(largestAdjacentNumbers(grid, 13));

问题:

所以问题是,无论我做什么,我的代码都停留在获取:
5377010688

正确答案是:
23514624000

正如您所看到的,这是一个巨大的差异。如果我只是在网格中做一个 4 相邻数字,它就会工作,因为它与 4 相邻数字示例给出的解决方案相匹配。这是:

5832

<小时/> 我确实研究了与欧拉项目问题 8 相关的其他一些 SO 问题,但似乎没有一个与我的问题相符。如果我确实错过了一个链接,那就太好了。尽管我确实对此表示怀疑,因为在发布此内容之前我实际上已经解决了很多问题。

<小时/>

问题状态:

https://projecteuler.net/problem=8 -- 问题链接

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

最佳答案

问题说:

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

(some number over multiple lines, 1000 digits long)

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

它并不是要求您迭代每一 - 它要求您将整个事物解释为一个字符串(或数字)并比较相邻的数字。最初将 grid 转换为单个字符串,您的代码将按预期工作:

var grid = `73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450`
    .split('\n').join('');

/**
 * Largest Adjacent Numbers
 * 
 * @returns int
 */
function largestAdjacentNumbers(grid, consecutiveLength = 4, separator = "\n") {
  let gridData = grid.split("\n"),
    largestProduct = 0;

  for (var row in gridData) {
    currentRow = gridData[row].split('').map(x => parseInt(x));

    for (var i = 0; i < currentRow.length - consecutiveLength; i++) {
      combination = currentRow.slice(i, i + consecutiveLength);
      if (!combination.includes(0)) {
        product = combination.reduce(function(a, b) {
          return a * b
        });
        if (largestProduct < product) largestProduct = product;
      }
    }
  }

  return largestProduct;
}

console.log(largestAdjacentNumbers(grid, 13));

但是,既然输入中没有行或换行符,最好删除在每一行上迭代的部分,并修复离一错误,如评论中所述:

var grid = `73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450`;
var gridData = grid.split('\n').join('').split('').map(Number);
/**
 * Largest Adjacent Numbers
 * 
 * @returns int
 */
function largestAdjacentNumbers(grid, consecutiveLength = 4, separator = "\n") {
  let largestProduct = 0;

  for (var i = 0; i <= gridData.length - consecutiveLength; i++) {
    combination = gridData.slice(i, i + consecutiveLength);
    if (!combination.includes(0)) {
      const product = combination.reduce(function(a, b) {
        return a * b
      });
      if (largestProduct < product) largestProduct = product;
    }
  }

  return largestProduct;
}

console.log(largestAdjacentNumbers(grid, 13));

关于javascript - 欧拉项目#8——Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51073915/

相关文章:

javascript - 如何在后台获取 chrome 扩展内容脚本?

javascript - 如何重置 ace 主题上的滚动条?

javascript - 修复了带有 jQ​​uery 可拖动的鼠标指针

javascript - 这个函数的堆栈会是什么样子?

javascript - HTML 中的制表符空间

javascript - 单击 anchor 子元素时防止重定向

javascript - jQuery 在同一行中是第一个而不是第一个,可能吗?

javascript - 无法使用谷歌图表适应模式窗口

javascript - 如何检查两个 Map 对象是否相等?

javascript - 有没有办法添加自定义方法来断言 chai 接口(interface)?