如何使用JavaScript在SPOJ上提交解决方案?

时间:2019-04-11 20:05:26

标签: javascript node.js algorithm spidermonkey onlinejudge

如何正确提交我的解决方案?我正在尝试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');
}

1 个答案:

答案 0 :(得分: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
}