程序语言为NodeJS
要做的步骤:1.变量输入具有输入值。从输入中提取n1和n2。2.写一个函数,求n1和n2的所有倍数之和,小于1000,包括1000。此函数应在2秒钟后记录总和。3.为事件发射器的任何实例注册一个名为MyEvent的事件,并将一个名名为logInfo的函数绑定到它,该函数将"{n1}和{n2}的倍数"记录到控制台并发射事件(在发射事件时不要传递任何参数)。限制
- Input:Input,一个用空格分隔的字符串
- 输出:用换行符分隔的字符串
注意:即使您得到了确切的输出,如果您不使用问题语句中提到的回调和事件概念,测试用例也会失败。
样本案例0
自定义测试的示例输入100 1000样本输出:100&10006500
解释100的倍数是100200300,。。。。。。1000和1000的倍数在1000以下并且包括1000。
总和=(100+200+…………1000)+1000
总和=6500
示例案例1
自定义测试的样本输入
500 1200
样本输出:
500&1200
1500
我尝试了以下代码:
process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
input += chunk;
});
process.stdin.on("end", function () {
let _input = input.split (" ");
let a = parseInt(_input[0]);
let b = parseInt(_input[1]);
console.log("Multiples of " + a + " & " + b);
var sum = 0;
for (var x = 0; x < 1000; x++)
{
if (x % a === 0 || x % b === 0)
{
sum += x;
}
}
console.log(sum);
});
代码测试:
案例1
输入(stdin)
4 6
您的输出(stdout)
4&6
165834
预期输出
4&6
208666
==============================================
情况2
输入(stdin)
3 5
您的输出(stdout)
3的倍数&5
233168
预期输出
3的倍数&5
267333
请帮我获取此代码。我的输出与这个逻辑不匹配。
我得到了正确的解决方案。
process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
input += chunk;
});
process.stdin.on("end", function () {
const EventEmitter = require('events');
let data = input.split(" ");
n1 = data[0];
n2 = data[1];
// console.log(n1,n2);
let result = 0;
const sum = () => {
for (let i = 0; i <= 1000; i++) {
if (i % n1 == 0) {
result += i;
}
}
for (let j = 0; j <= 1000; j++) {
if (j % n2 == 0) {
result += j;
}
}
console.log(result);
}
setTimeout(sum, 2000);
const event = new EventEmitter();
event.on("MyEvent", () => {
console.log(`Multiples of ${n1} & ${n2}`);
})
event.emit("MyEvent");
});
process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
input += chunk;
});
process.stdin.on("end", function () {
const myArr = input.split(" ");
const EventEmitter = require('events');
var eventEmitter = new EventEmitter();
let sum=0;
let a = myArr[0];
let b = myArr[1];
for(let i=3; i<=1000; i++){
if(i%a==0){
sum += i
}
if(i%b==0){
sum+=i
}
}
eventEmitter.on('MyEvent', function(){
setTimeout(function(){
console.log("Multiples of " + a + " & "+ b);
console.log(sum);},5000);
});
eventEmitter.emit('MyEvent');
});
我希望下面的代码能对您有所帮助,并给出预期的输出。谢谢!:)
process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
input += chunk;
});
process.stdin.on("end", function () {
const myArr = input.split(" ");
const EventEmitter = require('events');
var eventEmitter = new EventEmitter();
let sum=0;
let a = myArr[0];
let b = myArr[1];
for(let i=3; i<=1000; i++){
if(i%a==0){
sum += i
}
if(i%b==0){
sum+=i
}
}
eventEmitter.on('MyEvent', logInfo);
function logInfo(input) {
console.log("Multiples of " + a + " & "+ b);
console.log(sum);
}
eventEmitter.emit('MyEvent', "");
});
检查以下解决方案
process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
input += chunk;
});
process.stdin.on("end", function () {
const myArr = input.split(" ");
const EventEmitter = require('events');
var eventEmitter = new EventEmitter();
let sum=0;
let a = myArr[0];
let b = myArr[1];
for(let i=3; i<=1000; i++){
if(i%a==0){
sum += i
}
if(i%b==0){
sum+=i
}
}
eventEmitter.on('MyEvent', logInfo);
function logInfo(input) {
console.log("Multiples of " + a + " & "+ b);
console.log(sum);
}
eventEmitter.emit('MyEvent', "");
});
我只调用控制台作为异步函数,请避免我的错误。
process.stdin.on("end", function () {
// Enter your code here
const myArr = input.split(" ");
const EventEmitter = require('events');
var eventEmitter = new EventEmitter();
let sum=0;
let a = myArr[0];
let b = myArr[1];
for(let i=3; i<=1000; i++){
if(i%a==0){
sum += i
}
if(i%b==0){
sum+=i
}
}
setTimeout(function(){
console.log(sum);
},2000)
eventEmitter.on('MyEvent', logInfo);
function logInfo() {
console.log("Multiples of " + a + " & "+ b);
}
eventEmitter.emit('MyEvent');
});