华为机试实战OD

华为机试

`acm模式 1.微服务的集成测试

const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
​
const lines = [];
let n;
rl.on("line", (line) => {
  lines.push(line);
​
  if (lines.length === 1) {
    n = lines[0] - 0;
  }
​
  if (n && lines.length === n + 2) {
      //输入获取完毕
     
    lines.shift();
    const k = lines.pop();
      //二维数组 得到服务依赖数组
      
    const matrix = lines.map((line) => line.split(" ").map(Number));
      
    console.log(getResult(matrix, k, n));
      //释放空间
    lines.length = 0;
  }
});
​
function getResult(matrix, k, n) {
  // pre用于保存每个点的前驱点
  const pre = {};
  // 开始统计
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
      if (i == j) continue;
  if (!pre[i]) pre[i] = [];
 
  // useTime[i][j] = 1表示服务i启动依赖服务j启动完成,即j的后继点是i;i的前驱点是j
  if (matrix[i][j] == 1) {
    pre[i].push(j);
  }
}
  }
​
  return dfs(k - 1, pre, matrix);
}
​
function dfs(k, pre, matrix) {
  if (!pre[k].length) {
    return matrix[k][k];
  }
​
  const times = [];
  for (let p of pre[k]) {
    times.push(dfs(p, pre, matrix));
  }
​
  return matrix[k][k] + Math.max(...times);
}

2.对称美学

/* JavaScript Node ACM模式 控制台输入获取 */
const readline = require("readline");
​
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
​
const lines = [];
let t;
rl.on("line", (line) => {
  lines.push(line);
​
  if (lines.length === 1) {
    t = lines[0] - 0;
  }
​
  if (t && lines.length === t + 1) {
    lines.shift();
    const arr = lines.map((line) => line.split(" ").map(Number));
    getResult(arr);
​
    lines.length = 0;
​
  }
});
​
function getResult(arr) {
  for (let [n, k] of arr) {
    console.log(getNK(n, k )); 
  }
}
​
function getNK(n, k) {

  if (n === 1) {
    return "red";
  }
let len = Math.pow(2, n-1);
if (k >= len / 2) {
    let idx = k - len / 2;
    return getNK(n - 1, idx);
} else {
    return getNK(n - 1, k) == 'red' ? 'blue' : 'red';
}

} 3.最佳对手

/* JavaScript Node ACM模式 控制台输入获取 */
const readline = require("readline");
​
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
​
const lines = [];
rl.on("line", (line) => {
  lines.push(line);
​
  if (lines.length === 2) {
    const [n, d] = lines[0].split(" ").map(Number);
    const arr = lines[1].split(" ").map(Number);
    console.log(getResult(n, d, arr));
    lines.length = 0;
  }
});
function getResult(n, d, arr) {
  arr.sort((a, b) => a - b);
​
  const diffs = [];
​
  for (let i = 1; i < arr.length; i++) {
    const diff = arr[i] - arr[i - 1];
    if (diff <= d) {
      diffs.push([i - 1, i, diff]);
    }
  }
​
  if (diffs.length == 0) {
    return -1;
  }
​
  const res = [];
  dfs(0, diffs, [], res);
​
  res.sort((a, b) => (a[0] === b[0] ? a[1] - b[1] : b[0] - a[0]));
​
  return res[0][1];
}
​
​
  function dfs(index, diffs, path, res) {
  for (let i = index; i < diffs.length; i++) {
    if (path.length == 0 || path[path.length-1][1] < diffs[i][0]) {
      path.push(diffs[i]);
      dfs(i + 1, diffs, path, res);
      const count = path.length;
  const sumDiff = path.map((e) => e[2]).reduce((p, c) => p + c);
 
  res.push([count, sumDiff]);
 
  path.pop();
}
  }`

你可能感兴趣的:(学习的小结,javascript)