火星文计算值
题目:
var numArr = [];
var optionStrArr = [];
function haveBothStr(str) {
return str.indexOf("#") > -1 && str.indexOf("$") > -1;
}
function getArr(str, f) {
let arr = [];
arr[0] = str.substring(0, str.indexOf(f));
arr[1] = str.substring(str.indexOf(f) + 1);
return arr;
}
function main(line) {
let nowLine = line;
let arrTemp = [];
if (haveBothStr(nowLine)) {
if (line.indexOf("#") < line.indexOf("$")) {
optionStrArr.push("#");
arrTemp = getArr(nowLine, "#");
} else {
optionStrArr.push("$");
arrTemp = getArr(nowLine, "$");
}
numArr.push(arrTemp[0]);
nowLine = arrTemp[1];
} else {
if (line.indexOf("#") > -1) {
optionStrArr.push("#");
arrTemp = getArr(nowLine, "#");
} else if (line.indexOf("$") > -1) {
optionStrArr.push("$");
arrTemp = getArr(nowLine, "$");
} else {
numArr.push(nowLine);
jisuan();
}
numArr.push(arrTemp[0]);
nowLine = arrTemp[1];
}
if (nowLine.indexOf("#") > -1 || nowLine.indexOf("$") > -1) {
main(nowLine);
} else {
numArr.push(nowLine);
jisuan();
}
}
var sum = 0;
function jisuan() {
let index = -1;
if (optionStrArr.length > 0) {
if (optionStrArr.indexOf("$") > -1) {
index = optionStrArr.indexOf("$");
x = parseInt(numArr[index]);
y = parseInt(numArr[index + 1]);
sum = 3 * x + y + 2;
} else if (optionStrArr.indexOf("#") > -1) {
index = optionStrArr.indexOf("#");
x = parseInt(numArr[index]);
y = parseInt(numArr[index + 1]);
sum = 2 * x + 3 * y + 4;
} else {
console.log(sum);
}
numArr.splice(index, 2, sum);
optionStrArr.splice(index, 1);
} else {
console.log(sum);
}
if (optionStrArr.length > 0) {
jisuan();
} else {
console.log(sum);
}
}
main("7#6$5#12");//226
javaScript-node
var readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function(line){
main(line);
});
var numArr = [];
var optionStrArr = [];
function haveBothStr(str) {
return str.indexOf("#") > -1 && str.indexOf("$") > -1;
}
function getArr(str, f) {
let arr = [];
arr[0] = str.substring(0, str.indexOf(f));
arr[1] = str.substring(str.indexOf(f) + 1);
return arr;
}
function main(line) {
let nowLine = line;
let arrTemp = [];
if (haveBothStr(nowLine)) {
if (line.indexOf("#") < line.indexOf("$")) {
optionStrArr.push("#");
arrTemp = getArr(nowLine, "#");
} else {
optionStrArr.push("$");
arrTemp = getArr(nowLine, "$");
}
numArr.push(arrTemp[0]);
nowLine = arrTemp[1];
} else {
if (line.indexOf("#") > -1) {
optionStrArr.push("#");
arrTemp = getArr(nowLine, "#");
} else if (line.indexOf("$") > -1) {
optionStrArr.push("$");
arrTemp = getArr(nowLine, "$");
} else {
numArr.push(nowLine);
jisuan();
}
numArr.push(arrTemp[0]);
nowLine = arrTemp[1];
}
if (nowLine.indexOf("#") > -1 || nowLine.indexOf("$") > -1) {
main(nowLine);
} else {
numArr.push(nowLine);
jisuan();
}
}
var sum = 0;
function jisuan() {
let index = -1;
if (optionStrArr.length > 0) {
if (optionStrArr.indexOf("$") > -1) {
index = optionStrArr.indexOf("$");
let x = parseInt(numArr[index]);
let y = parseInt(numArr[index + 1]);
sum = 3 * x + y + 2;
} else if (optionStrArr.indexOf("#") > -1) {
index = optionStrArr.indexOf("#");
let x = parseInt(numArr[index]);
let y = parseInt(numArr[index + 1]);
sum = 2 * x + 3 * y + 4;
} else {
console.log(sum);
}
numArr.splice(index, 2, sum);
optionStrArr.splice(index, 1);
} else {
console.log(sum);
}
if (optionStrArr.length > 0) {
jisuan();
} else {
console.log(sum);
}
}
接雨水
function main(height) {
var rain = 0
for (let i = 1; i < height.length - 1; i++) {
var max_left = 0
for (let j = i - 1; j >= 0; j--) {
if (height[j] > max_left) {
max_left = height[j]
}
}
var max_right = 0
for (let j = i + 1; j < height.length; j++) {
if (height[j] > max_right) {
max_right = height[j]
}
}
var min = Math.min(max_left, max_right)
if (min > height[i]) {
rain = rain + (min - height[i])
}
}
return rain
}
main( [0,1,0,2,1,0,1,3,2,1,2,1]) //6