Algorithm
Problem Name: beecrowd | 3241
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/3241
Help a PhD Candidate Out!
By Christian Jonassen Norway
Timelimit: 1
Jon Marius forgot how to add two numbers while doing research for his PhD. And now he has a long list of addition problems that he needs to solve, in addition to his computer science ones! Can you help him?
On his current list Jon Marius has two kinds of problems: addition problems on the form ”a+b” and the ever returning problem ”P=NP”. Jon Marius is a quite distracted person, so he might have so solve this last problem several times, since he keeps forgetting the solution. Also, he would like to solve these problems by himself, so you should skip these.
Input
The first line of input will be a single integer N (1 ≤ N ≤ 1000) denoting the number of testcases. Then follow N lines with either ”P=NP” or an addition problem on the form ”a + b”, where a, b ∈ [0, 1000] are integers.
Output
Output the result of each addition. For lines containing “P=NP”, output “skipped”.
Input Sample | Output Sample |
4 |
4 |
Code Examples
#1 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("node:fs")
const [numLines, ...input] = readFileSync("/dev/stdin", "utf8").split("\n", 1000 + 1)
function evaluateSimpleLinearExpression(expression = "") {
while (expression.includes("("))
expression = expression
.replace(/\(([\d+\-*/ ]+?)\)/g, (_, op) => String(evaluateSimpleLinearExpression(op)))
expression = expression
.replace(/(-?\d+)\s*?([*/])\s?(-?\d+)/g, (_, numA, operator, numB) => {
if (operator === "*") return String(Number.parseInt(numA, 10) * Number.parseInt(numB, 10))
if (operator === "/") return String(Number.parseInt(numA, 10) / Number.parseInt(numB, 10))
})
.replace(/(-?\d+)\s*?([+-])\s*?(-?\d+)/g, (_, numA, operator, numB) => {
if (operator === "+") return String(Number.parseInt(numA, 10) + Number.parseInt(numB, 10))
if (operator === "-") return String(Number.parseInt(numA, 10) - Number.parseInt(numB, 10))
})
return Number(expression)
}
function main() {
const output = new Array(Number.parseInt(numLines, 10))
for (let index = 0; index < output.length; index++) {
const line = input[index]
output[index] = (line === "P=NP") ? "skipped" : evaluateSimpleLinearExpression(line)
}
console.log(output.join("\n"))
}
main()
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
var prompt = function(texto){ return lines.shift(); };
let count = parseInt(prompt("quantidade e vezes")); x = 0, y = 0;
for(count; count > 0; count--) {
let soma = prompt("soma");
if (soma != 'P=NP') {
soma = soma.split('+');
x = parseInt(soma.shift());
y = parseInt(soma.shift());
soma = x + y;
console.log(soma);
} else {
console.log('skipped');
}
soma = 0;
}
Copy The Code &
Try With Live Editor
Input
Output