如何在JavaScript中提交SPOJ解决方案?

问题描述 投票:0回答:1

我该如何正确提交我的解决方案?我正在尝试console.logs和返回,所有这些都失败了一些错误,如编译错误,NZEC,即使它是从堆栈溢出复制和粘贴的实现...我尝试使用Node,JS Rihno和Spider Monkey,所有他们失败了......

function isPrime(number) {
  if (number <= 1)
     print('NO');

  // The check for the number 2 and 3
  if (number <= 3)
     print('YES');

  if (number % 2 == 0 || number % 3 == 0)
     print('NO');

  for (var i = 5; i * i <= number; i = i + 6) {
    if (number % i == 0 || number % (i + 2) == 0)
       print('NO');
  }

   print('YES');
}
javascript node.js algorithm spidermonkey onlinejudge
1个回答
0
投票

我用这种方式解决了这个问题

let n = 1;
let answersArray;

(function () {
    const readline = require('readline');

    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });

    rl.question('', (input) => {
        n = Number(input) || 0
        prepareArray()
    });

    rl.on('line', (input) => {
        n = n - 1;
        const index = n
        isPrimeNumber(input, index)
        n === 0 ? showAnswer() : null
    });

})()


function prepareArray() {
    answersArray = new Array(n).fill(0);
}

function showAnswer() {
    for (let i = answersArray.length - 1; i >= 0; i--) {
        answersArray[i] ? console.log('TAK') : console.log('NIE')
    }
}

function isPrimeNumber(number, index) {
    let isPrime = true
    if (number < 2) isPrime = false
    else {
        for (let i = 2; i * i <= number; i++)
            if (number % i === 0) isPrime = false
    }
    answersArray[index] = isPrime
}
© www.soinside.com 2019 - 2024. All rights reserved.