前端算法2-困难

// HJ3 明明的随机数
while ((line = readline())) {
  let arr = [];
  for (let i = 0; i < +line; i++) {
    arr.push(readline());
  }
  [...new Set(arr)]
    .sort((a, b) => a - b)
    .forEach((el) => {
      print(el);
    });
}

// HJ18 识别有效的IP地址和掩码并进行分类统计
function checkMark(mrr) {
  let bit = "";
  mrr.forEach((el) => {
    bit += (parseInt(el) + 256).toString(2).substr(1);
  });
  return (
    bit.indexOf("01") === -1 && bit.indexOf("0") > -1 && bit.indexOf("1") > -1
  );
}
const ipReg = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
let crr = [0, 0, 0, 0, 0, 0, 0];
while ((line = readline())) {
  let ipArr = line.split("~");
  const srr = ipArr[0].split(".").map(Number);
  const mrr = ipArr[1].split(".").map(Number);
  if (srr[0] == 0 || srr[0] == 127) {
  } else if (!checkMark(mrr)) {
    crr[5]++;
  } else if (!ipReg.test(ipArr[0])) {
    crr[5]++;
  } else {
    if (
      srr[0] == 10 ||
      (srr[0] == 172 && srr[1] > 15 && srr[1] < 32) ||
      (srr[0] == 192 && srr[1] == 168)
    ) {
      crr[6]++;
    }
    if (srr[0] > 0 && srr[0] < 127) {
      crr[0]++;
    } else if (srr[0] > 127 && srr[0] < 192) {
      crr[1]++;
    } else if (srr[0] > 191 && srr[0] < 224) {
      crr[2]++;
    } else if (srr[0] > 223 && srr[0] < 240) {
      crr[3]++;
    } else if (srr[0] > 239 && srr[0] < 256) {
      crr[4]++;
    }
  }
}
print(crr.join(" "));

// HJ19 简单错误记录
let obj = {};
let str;
while ((str = readline())) {
  const [c, n] = str.split(" ");
  const s = c.substr(c.lastIndexOf("\\") + 1).substr(-16);
  obj[`${s} ${n}`] = (obj[`${s} ${n}`] || 0) + 1;
}

Object.keys(obj)
  .slice(-8)
  .forEach((el) => {
    print(`${el} ${obj[el]}`);
  });

// HJ25 数据分类处理
while ((str = readline())) {
  //当能读取输入的时候进入循环
  let IArr = str.split(" "); //拆分得到I序列
  let RArr = readline().split(" "); //拆分得到R序列
  IArr.shift(); //删除数组第一位,下同
  RArr.shift();
  RArr = [...new Set(RArr.sort((a, b) => a - b))]; //利用Set去重,同时排序,重新赋值给RArr
  let log = []; //定义输出数组
  RArr.forEach((item) => {
    //对R序列的每一项
    let itemArr = []; //定义临时数组
    for (let i = 0; i < IArr.length; i++) {
      if (IArr[i].includes(item)) {
        //查找包含R单项的I序列项
        itemArr.push(i, IArr[i]); //若找到,在临时数组中保存下标和I序列单项
      }
    }
    if (itemArr.length) {
      //如果临时数组长度大于0
      log.push(item, itemArr.length / 2, ...itemArr); //输出数组中加入R单项,I序列中符合条件的个数,并把保存的临沭数组扩展进来
    }
  });
  log.unshift(log.length); //统计输出数组的长度,并将该数字添加到输出数组的第一位
  console.log(log.join(" "));
}

// HJ30 字符串合并处理
while ((line = readline())) {
  let result = "";
  // step 1
  let str = line.split(" ").join("").split("");
  let len = str.length;
  // step 2
  let idx = 0;
  let oddArr = [];
  let evenArr = [];
  while (idx !== len) {
    if (idx % 2 === 0) {
      evenArr.push(str[idx]);
    } else {
      oddArr.push(str[idx]);
    }
    idx++;
  }
  oddArr.sort();
  evenArr.sort();
  while (idx !== 0) {
    result += (len - idx) % 2 === 0 ? evenArr.shift() : oddArr.shift();
    idx--;
  }

  // step 3
  let last = "";
  while (idx !== len) {
    let i = result[idx];
    if ((i > "F" && i < "a") || i > "f") {
      last += i;
    } else {
      let temp = [...parseInt(i, 16).toString(2).padStart(4, "0")]
        .reverse()
        .join("");
      last += parseInt(temp, 2).toString("16").toUpperCase();
    }
    idx++;
  }
  console.log(last);
}

//   HJ39 判断两个IP是否属于同一子网
while ((line = readline())) {
  let [a, b, c] = [line, readline(), readline()];
  if (isIp(a) & isIp(b) & isIp(c)) {
    let yArr = toPad(a);
    if (isIpMask(yArr)) {
      let p1Arr = toPad(b);
      let p2Arr = toPad(c);
      if (getAnd(p1Arr, yArr) == getAnd(p2Arr, yArr)) {
        print(0);
      } else {
        print(2);
      }
    } else {
      print(1);
    }
  } else {
    print(1);
  }
}
function toPad(ip) {
  return ip.split(".").map((el) => Number(el).toString(2).padStart(8, "0"));
}
function isIp(ip) {
  let ipArr = ip.split(".");
  let isOk = true;
  ipArr.forEach((el) => {
    if (Number(el) > 255) {
      isOk = false;
    }
  });
  return isOk && /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.test(ip);
}
function isIpMask(ipm) {
  return !ipm.join("").includes("01");
}
function getAnd(p1, p2) {
  let i = 0;
  let pArr = "";
  let tmp = [];
  while (i < 4) {
    let a1 = p1[i].split("");
    let a2 = p2[i].split("");
    let j = 0;
    while (j < 8) {
      if (a1[j] == 0 || a2[j] == 0) {
        tmp.push(0);
      } else {
        tmp.push(1);
      }
      j++;
    }
    pArr += tmp.join("");
    i++;
  }
  return pArr;
}

//   HJ42 学英语
let line;
let bitsmap = {
  1: "one",
  2: "two",
  3: "three",
  4: "four",
  5: "five",
  6: "six",
  7: "seven",
  8: "eight",
  9: "nine",
};
let tenmap = {
  1: "ten",
  2: "twenty",
  3: "thirty",
  4: "forty",
  5: "fifty",
  6: "sixty",
  7: "seventy",
  8: "eighty",
  9: "ninety",
};
//
let hbit = "hundred";
//
let tbit = "thousand";
let mbit = "million";
let bbit = "billion";
let choose = [tbit, mbit, bbit];
while ((line = readline())) {
  line = line.replace(/(\d)(?=((\d{3})+(\.|$)))/g, "$1,");
  let bits = [];
  let bitArr = line.split(",").reverse();
  let result = "";
  bitArr.forEach((group) => {
    let res;
    let arr = group.split("");
    if (group.length === 3) {
      let tenb = tenmap[arr[1]];
      let bit = bitsmap[arr[2]];
      if (arr[0] === "0") {
        res = `${tenb || ""} ${bit || ""}`;
      } else {
        if (tenb || bit) {
          res = `${bitsmap[arr[0]] || ""} ${hbit} and ${tenb || ""} ${
            bit || ""
          }`;
        } else {
          res = `${bitsmap[arr[0]] || ""} ${hbit}`;
        }
      }
    } else if (group.length === 2) {
      let tenb = tenmap[arr[0]];
      let bit = bitsmap[arr[1]];
      if (tenb) {
        if (bit) {
          res = `${tenb} ${bit || ""}`;
        } else {
          res = tenb;
        }
      } else {
        res = bitsmap[arr[1]] || "";
      }
      //             console.log(arr)
      //             console.log(res)
    } else if (group.length === 1) {
      res = bitsmap[group] || "";
    }
    if (bits.length) {
      result = res + " " + choose[bits.length - 1] + " " + result;
    } else {
      result = res + " " + result;
    }
    bits.push(",");
  });
  result = result.replace(/\s+/g, " ");
  result = result.replace(/ten five/g, "fifteen");
  console.log(result);
}

// HJ68 成绩排序
let line;

let arr = [];

const getSequence = (arr, order) => {
  let scoreArr = [];
  let result = [];
  //    将分数分离
  arr.forEach((item, index) => {
    let temp = item.split(" ");
    scoreArr.push({
      index: index,
      score: parseInt(temp[1]),
    });
  });
  result = scoreArr.reduce((list, item) => {
    let newList = list;
    if (newList.length == 0) {
      newList.push(item);
    } else {
      let index;
      if (order == 0) {
        index = newList.findIndex((i) => i.score < item.score);
      } else {
        index = newList.findIndex((i) => i.score > item.score);
      }
      index > -1 ? newList.splice(index, 0, item) : newList.push(item);
    }
    return newList;
  }, []);
  let res = [];
  result.forEach((item) => {
    let index = item.index;
    res.push(arr[index]);
  });
  return res;
};
while ((line = readline())) {
  arr.push(line);
  if (arr.length >= 2) {
    let count = parseInt(arr[0]);
    let direction = parseInt(arr[1]); //0 从高到低 1 从低到高
    if (arr.length == 2 + count) {
      let dict = JSON.parse(JSON.stringify(arr));
      dict.splice(0, 2);
      let newArr = [];
      let result = getSequence(dict, direction);
      result.map((i) => console.log(i));
      arr = [];
    }
  }
}

// HJ88 扑克牌大小
while ((line = readline())) {
  // 通过数组的索引确定牌的大小顺序
  const compare = [
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "T",
    "J",
    "Q",
    "K",
    "A",
    "2",
  ];

  // 得到两副牌
  const arr = line.split("-");
  let [x, y] = [arr[0], arr[1]];

  if (
    (x.indexOf("j") !== -1 && x.indexOf("JO") !== -1) ||
    (y.indexOf("j") !== -1 && y.indexOf("JO") !== -1)
  ) {
    // 如果有副牌中有大小王,则直接输出大小王
    console.log("joker JOKER");
  } else if (x.indexOf("JO") !== -1 || y.indexOf("JO") !== -1) {
    // 如果有副牌中有大小王,则直接输出大小王
    console.log("JOKER");
  } else if (
    (x.indexOf("j") !== -1 && y.indexOf("JO") === -1) ||
    (y.indexOf("j") !== -1 && x.indexOf("JO") === -1)
  ) {
    // 如果有副牌中有大小王,则直接输出大小王
    console.log("joker");
  } else {
    // 将每张牌中间的空格去除,以方便获取牌的个数
    // 将牌面 10 换成 T -> 因为当类型相同时,需要比较第一个字符确定牌面大小,如果是 10 的话,就会取到 1
    let xTemp = x.replace(/10/g, "T").replace(/ /g, "");
    let yTemp = y.replace(/10/g, "T").replace(/ /g, "");

    // 得到两副牌的个数
    let [xTempLen, yTempLen] = [xTemp.length, yTemp.length];

    if (xTempLen !== yTempLen) {
      // 两副牌个数不相同
      if (xTempLen === 4) {
        // 第一副牌是炸弹,输出第一副牌
        console.log(x);
      } else if (yTempLen === 4) {
        // 第二副牌是炸弹,输出第二副牌
        console.log(y);
      } else {
        // 两副牌都没炸弹,类型不同,无法比较
        console.log("ERROR");
      }
    } else {
      // 两副牌个数相同 -> 类型相同
      if (compare.indexOf(xTemp[0]) > compare.indexOf(yTemp[0])) {
        // 第一副牌的牌面大
        console.log(x);
      } else {
        console.log(y);
      }
    }
  }
}

// HJ89 24点运算
// 4张扑克牌 空格隔开,大小王ERROR
// 1 - 13
// 没有括号
// 顺序任意
// 运算从左往右
// 无解输出NONE
let line = null;
while ((line = readline())) {
  if (line.length > 9) {
    print("ERROR");
    continue;
  }

  let nums = line.split(" ").map((a) => {
    if (a == "A") {
      return 1;
    } else if (a == "J") {
      return 11;
    } else if (a == "Q") {
      return 12;
    } else if (a == "K") {
      return 13;
    } else {
      return parseInt(a);
    }
  });

  let ns = getNums(nums);
  for (let i = 0; i < ns.length; i++) {
    let r = calc24(null, ns[i], ["+", "-", "*", "/"]);
    if (r) {
      print(getS(ns[i][0]) + r);
      break;
    } else if (i == ns.length - 1) {
      print("NONE");
    }
  }
}

function getNums(numbers) {
  let result = [];
  for (let i = 0; i < numbers.length; i++) {
    let num = numbers[i];
    let nums = numbers.slice();
    nums.splice(i, 1);
    //console.log('===' + num + 'dd' + nums + 'kk' + numbers);
    let item = [num];
    if (nums.length > 0) {
      let r = getNums(nums);
      for (let j = 0; j < r.length; j++) {
        result.push(item.concat(r[j]));
      }
      //result.push(item.concat(getNums(nums)));
    } else {
      result.push(item);
    }
  }

  return result;
}

function calc24(result, numbers, symbols) {
  let nums = numbers.slice();
  if (result == null) {
    result = nums.shift();
  }

  let num = nums.shift();
  //console.log(num, 'tt', nums, symbols)
  //print('ddd' + result + 'tt' + num + 'ss' + nums)

  //print('ccrr', result, 'nums', numbers, 'ss', symbols)
  if (result)
    for (let i = 0; i < symbols.length; i++) {
      let s = symbols[i];
      let cr = result;
      if (s == "+") {
        cr = cr + num;
      } else if (s == "-") {
        //print('----' + cr + '----' + num)
        cr = cr - num;
      } else if (s == "*") {
        //print('****' + cr + '****' + num)
        cr = cr * num;
        //print('cr', cr)
      } else if (s == "/") {
        //print('___' + cr + '___' + num)
        cr = Math.floor(cr / num);
      }

      if (nums.length > 0) {
        let r = calc24(cr, nums, symbols); //symbols.filter(a => a != s));
        if (r) {
          return s + getS(num) + r;
        } else {
          continue;
        }
      } else {
        //print('qqqqqqqq', cr)
        if (cr == 24) {
          return s + getS(num);
        } else {
          continue;
        }
      }
    }

  return null;
}

function getS(num) {
  if (num == 1) {
    return "A";
  } else if (num == 11) {
    return "J";
  } else if (num == 12) {
    return "Q";
  } else if (num == 13) {
    return "K";
  } else {
    return num + "";
  }
} // 4张扑克牌 空格隔开,大小王ERROR
// 1 - 13
// 没有括号
// 顺序任意
// 运算从左往右
// 无解输出NONE
let line = null;
while ((line = readline())) {
  if (line.length > 9) {
    print("ERROR");
    continue;
  }

  let nums = line.split(" ").map((a) => {
    if (a == "A") {
      return 1;
    } else if (a == "J") {
      return 11;
    } else if (a == "Q") {
      return 12;
    } else if (a == "K") {
      return 13;
    } else {
      return parseInt(a);
    }
  });

  let ns = getNums(nums);
  for (let i = 0; i < ns.length; i++) {
    let r = calc24(null, ns[i], ["+", "-", "*", "/"]);
    if (r) {
      print(getS(ns[i][0]) + r);
      break;
    } else if (i == ns.length - 1) {
      print("NONE");
    }
  }
}

function getNums(numbers) {
  let result = [];
  for (let i = 0; i < numbers.length; i++) {
    let num = numbers[i];
    let nums = numbers.slice();
    nums.splice(i, 1);
    //console.log('===' + num + 'dd' + nums + 'kk' + numbers);
    let item = [num];
    if (nums.length > 0) {
      let r = getNums(nums);
      for (let j = 0; j < r.length; j++) {
        result.push(item.concat(r[j]));
      }
      //result.push(item.concat(getNums(nums)));
    } else {
      result.push(item);
    }
  }

  return result;
}

function calc24(result, numbers, symbols) {
  let nums = numbers.slice();
  if (result == null) {
    result = nums.shift();
  }

  let num = nums.shift();
  //console.log(num, 'tt', nums, symbols)
  //print('ddd' + result + 'tt' + num + 'ss' + nums)

  //print('ccrr', result, 'nums', numbers, 'ss', symbols)
  if (result)
    for (let i = 0; i < symbols.length; i++) {
      let s = symbols[i];
      let cr = result;
      if (s == "+") {
        cr = cr + num;
      } else if (s == "-") {
        //print('----' + cr + '----' + num)
        cr = cr - num;
      } else if (s == "*") {
        //print('****' + cr + '****' + num)
        cr = cr * num;
        //print('cr', cr)
      } else if (s == "/") {
        //print('___' + cr + '___' + num)
        cr = Math.floor(cr / num);
      }

      if (nums.length > 0) {
        let r = calc24(cr, nums, symbols); //symbols.filter(a => a != s));
        if (r) {
          return s + getS(num) + r;
        } else {
          continue;
        }
      } else {
        //print('qqqqqqqq', cr)
        if (cr == 24) {
          return s + getS(num);
        } else {
          continue;
        }
      }
    }

  return null;
}

function getS(num) {
  if (num == 1) {
    return "A";
  } else if (num == 11) {
    return "J";
  } else if (num == 12) {
    return "Q";
  } else if (num == 13) {
    return "K";
  } else {
    return num + "";
  }
}

// HJ93 数组分组
while ((num = readline())) {
  let nums = readline()
    .trim()
    .split(" ")
    .map((i) => +i);
  let fs = 0,
    ts = 0,
    rs = 0,
    rl = [];
  for (let n of nums) {
    if (n % 5 == 0) {
      fs += n;
    } else if (n % 3 == 0) {
      ts += n;
    } else {
      rs += n;
      rl.push(n);
    }
  }
  let hs = (fs + ts + rs) / 2;
  if (Math.floor(hs) != hs) {
    console.log(false);
  } else {
    console.log(devide(fs, ts, rl, 0));
  }
}
function devide(fs, ts, rl, index) {
  //递归
  if (index == rl.length) {
    return fs == ts;
  }
  return (
    devide(fs + rl[index], ts, rl, index + 1) ||
    devide(fs, ts + rl[index], rl, index + 1)
  );
}

//   HJ95 人民币转换
while ((line = readline())) {
  let number = line.split(".");
  let str = {
    0: "零",
    1: "壹",
    2: "贰",
    3: "叁",
    4: "肆",
    5: "伍",
    6: "陆",
    7: "柒",
    8: "捌",
    9: "玖",
  };
  let [zhengshu, xiaoshu] = number;
  let out = "";
  let temp = "";
  for (let i = zhengshu.length - 1; i >= 0; i--) {
    temp = zhengshu[i] + temp;
    if (temp.length == 4 || i == 0) {
      let s = "";
      if (zhengshu.length - i > 4 && zhengshu.length - i <= 8) {
        s = "万";
      } else if (zhengshu.length - i > 8 && zhengshu.length - i <= 12) {
        s = "亿";
      }
      out = get_four(temp) + s + out;
      temp = "";
    }
  }
  out = out.replace(/^0+/, "");
  out = out.replace(/0+$/, "");
  out = out.replace(/0+/g, "零");
  out = out.replace(/壹拾/g, "拾");
  out = out.replace(/零万/g, "万");
  out = "人民币" + (out == "" ? "" : out + "元") + get_two(xiaoshu);
  console.log(out);
  function get_four(num) {
    let out = "";
    num = num.padStart(4, "0");
    out =
      (num[0] == "0" ? "0" : str[num[0]] + "仟") +
      (num[1] == "0" ? "0" : str[num[1]] + "佰") +
      (num[2] == "0" ? "0" : str[num[2]] + "拾") +
      (num[3] == "0" ? "0" : str[num[3]]);
    return out;
  }
  function get_two(num) {
    if (num[0] == "0" && num[1] == "0") {
      return "整";
    } else {
      return (
        (num[0] == "0" ? "" : str[num[0]] + "角") +
        (num[1] == "0" ? "" : str[num[1]] + "分")
      );
    }
  }
}

// HJ105 记负均正II
let str;
let arr = [];

while ((str = readline())) {
  arr.push(Number(str));
}
let arr2 = arr.filter((item) => item >= 0);
let arr3 = arr.filter((item) => item < 0);
let avg = arr2.length > 0 ? arr2.reduce((t, v) => t + v, 0) / arr2.length : 0;

console.log(arr3.length);
console.log(avg.toFixed(1));
// HJ3 明明的随机数
while ((line = readline())) {
  let arr = [];
  for (let i = 0; i < +line; i++) {
    arr.push(readline());
  }
  [...new Set(arr)]
    .sort((a, b) => a - b)
    .forEach((el) => {
      print(el);
    });
}

// HJ18 识别有效的IP地址和掩码并进行分类统计
function checkMark(mrr) {
  let bit = "";
  mrr.forEach((el) => {
    bit += (parseInt(el) + 256).toString(2).substr(1);
  });
  return (
    bit.indexOf("01") === -1 && bit.indexOf("0") > -1 && bit.indexOf("1") > -1
  );
}
const ipReg = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
let crr = [0, 0, 0, 0, 0, 0, 0];
while ((line = readline())) {
  let ipArr = line.split("~");
  const srr = ipArr[0].split(".").map(Number);
  const mrr = ipArr[1].split(".").map(Number);
  if (srr[0] == 0 || srr[0] == 127) {
  } else if (!checkMark(mrr)) {
    crr[5]++;
  } else if (!ipReg.test(ipArr[0])) {
    crr[5]++;
  } else {
    if (
      srr[0] == 10 ||
      (srr[0] == 172 && srr[1] > 15 && srr[1] < 32) ||
      (srr[0] == 192 && srr[1] == 168)
    ) {
      crr[6]++;
    }
    if (srr[0] > 0 && srr[0] < 127) {
      crr[0]++;
    } else if (srr[0] > 127 && srr[0] < 192) {
      crr[1]++;
    } else if (srr[0] > 191 && srr[0] < 224) {
      crr[2]++;
    } else if (srr[0] > 223 && srr[0] < 240) {
      crr[3]++;
    } else if (srr[0] > 239 && srr[0] < 256) {
      crr[4]++;
    }
  }
}
print(crr.join(" "));

// HJ19 简单错误记录
let obj = {};
let str;
while ((str = readline())) {
  const [c, n] = str.split(" ");
  const s = c.substr(c.lastIndexOf("\\") + 1).substr(-16);
  obj[`${s} ${n}`] = (obj[`${s} ${n}`] || 0) + 1;
}

Object.keys(obj)
  .slice(-8)
  .forEach((el) => {
    print(`${el} ${obj[el]}`);
  });

// HJ25 数据分类处理
while ((str = readline())) {
  //当能读取输入的时候进入循环
  let IArr = str.split(" "); //拆分得到I序列
  let RArr = readline().split(" "); //拆分得到R序列
  IArr.shift(); //删除数组第一位,下同
  RArr.shift();
  RArr = [...new Set(RArr.sort((a, b) => a - b))]; //利用Set去重,同时排序,重新赋值给RArr
  let log = []; //定义输出数组
  RArr.forEach((item) => {
    //对R序列的每一项
    let itemArr = []; //定义临时数组
    for (let i = 0; i < IArr.length; i++) {
      if (IArr[i].includes(item)) {
        //查找包含R单项的I序列项
        itemArr.push(i, IArr[i]); //若找到,在临时数组中保存下标和I序列单项
      }
    }
    if (itemArr.length) {
      //如果临时数组长度大于0
      log.push(item, itemArr.length / 2, ...itemArr); //输出数组中加入R单项,I序列中符合条件的个数,并把保存的临沭数组扩展进来
    }
  });
  log.unshift(log.length); //统计输出数组的长度,并将该数字添加到输出数组的第一位
  console.log(log.join(" "));
}

// HJ30 字符串合并处理
while ((line = readline())) {
  let result = "";
  // step 1
  let str = line.split(" ").join("").split("");
  let len = str.length;
  // step 2
  let idx = 0;
  let oddArr = [];
  let evenArr = [];
  while (idx !== len) {
    if (idx % 2 === 0) {
      evenArr.push(str[idx]);
    } else {
      oddArr.push(str[idx]);
    }
    idx++;
  }
  oddArr.sort();
  evenArr.sort();
  while (idx !== 0) {
    result += (len - idx) % 2 === 0 ? evenArr.shift() : oddArr.shift();
    idx--;
  }

  // step 3
  let last = "";
  while (idx !== len) {
    let i = result[idx];
    if ((i > "F" && i < "a") || i > "f") {
      last += i;
    } else {
      let temp = [...parseInt(i, 16).toString(2).padStart(4, "0")]
        .reverse()
        .join("");
      last += parseInt(temp, 2).toString("16").toUpperCase();
    }
    idx++;
  }
  console.log(last);
}

//   HJ39 判断两个IP是否属于同一子网
while ((line = readline())) {
  let [a, b, c] = [line, readline(), readline()];
  if (isIp(a) & isIp(b) & isIp(c)) {
    let yArr = toPad(a);
    if (isIpMask(yArr)) {
      let p1Arr = toPad(b);
      let p2Arr = toPad(c);
      if (getAnd(p1Arr, yArr) == getAnd(p2Arr, yArr)) {
        print(0);
      } else {
        print(2);
      }
    } else {
      print(1);
    }
  } else {
    print(1);
  }
}
function toPad(ip) {
  return ip.split(".").map((el) => Number(el).toString(2).padStart(8, "0"));
}
function isIp(ip) {
  let ipArr = ip.split(".");
  let isOk = true;
  ipArr.forEach((el) => {
    if (Number(el) > 255) {
      isOk = false;
    }
  });
  return isOk && /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.test(ip);
}
function isIpMask(ipm) {
  return !ipm.join("").includes("01");
}
function getAnd(p1, p2) {
  let i = 0;
  let pArr = "";
  let tmp = [];
  while (i < 4) {
    let a1 = p1[i].split("");
    let a2 = p2[i].split("");
    let j = 0;
    while (j < 8) {
      if (a1[j] == 0 || a2[j] == 0) {
        tmp.push(0);
      } else {
        tmp.push(1);
      }
      j++;
    }
    pArr += tmp.join("");
    i++;
  }
  return pArr;
}

//   HJ42 学英语
let line;
let bitsmap = {
  1: "one",
  2: "two",
  3: "three",
  4: "four",
  5: "five",
  6: "six",
  7: "seven",
  8: "eight",
  9: "nine",
};
let tenmap = {
  1: "ten",
  2: "twenty",
  3: "thirty",
  4: "forty",
  5: "fifty",
  6: "sixty",
  7: "seventy",
  8: "eighty",
  9: "ninety",
};
//
let hbit = "hundred";
//
let tbit = "thousand";
let mbit = "million";
let bbit = "billion";
let choose = [tbit, mbit, bbit];
while ((line = readline())) {
  line = line.replace(/(\d)(?=((\d{3})+(\.|$)))/g, "$1,");
  let bits = [];
  let bitArr = line.split(",").reverse();
  let result = "";
  bitArr.forEach((group) => {
    let res;
    let arr = group.split("");
    if (group.length === 3) {
      let tenb = tenmap[arr[1]];
      let bit = bitsmap[arr[2]];
      if (arr[0] === "0") {
        res = `${tenb || ""} ${bit || ""}`;
      } else {
        if (tenb || bit) {
          res = `${bitsmap[arr[0]] || ""} ${hbit} and ${tenb || ""} ${
            bit || ""
          }`;
        } else {
          res = `${bitsmap[arr[0]] || ""} ${hbit}`;
        }
      }
    } else if (group.length === 2) {
      let tenb = tenmap[arr[0]];
      let bit = bitsmap[arr[1]];
      if (tenb) {
        if (bit) {
          res = `${tenb} ${bit || ""}`;
        } else {
          res = tenb;
        }
      } else {
        res = bitsmap[arr[1]] || "";
      }
      //             console.log(arr)
      //             console.log(res)
    } else if (group.length === 1) {
      res = bitsmap[group] || "";
    }
    if (bits.length) {
      result = res + " " + choose[bits.length - 1] + " " + result;
    } else {
      result = res + " " + result;
    }
    bits.push(",");
  });
  result = result.replace(/\s+/g, " ");
  result = result.replace(/ten five/g, "fifteen");
  console.log(result);
}

// HJ68 成绩排序
let line;

let arr = [];

const getSequence = (arr, order) => {
  let scoreArr = [];
  let result = [];
  //    将分数分离
  arr.forEach((item, index) => {
    let temp = item.split(" ");
    scoreArr.push({
      index: index,
      score: parseInt(temp[1]),
    });
  });
  result = scoreArr.reduce((list, item) => {
    let newList = list;
    if (newList.length == 0) {
      newList.push(item);
    } else {
      let index;
      if (order == 0) {
        index = newList.findIndex((i) => i.score < item.score);
      } else {
        index = newList.findIndex((i) => i.score > item.score);
      }
      index > -1 ? newList.splice(index, 0, item) : newList.push(item);
    }
    return newList;
  }, []);
  let res = [];
  result.forEach((item) => {
    let index = item.index;
    res.push(arr[index]);
  });
  return res;
};
while ((line = readline())) {
  arr.push(line);
  if (arr.length >= 2) {
    let count = parseInt(arr[0]);
    let direction = parseInt(arr[1]); //0 从高到低 1 从低到高
    if (arr.length == 2 + count) {
      let dict = JSON.parse(JSON.stringify(arr));
      dict.splice(0, 2);
      let newArr = [];
      let result = getSequence(dict, direction);
      result.map((i) => console.log(i));
      arr = [];
    }
  }
}

// HJ88 扑克牌大小
while ((line = readline())) {
  // 通过数组的索引确定牌的大小顺序
  const compare = [
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "T",
    "J",
    "Q",
    "K",
    "A",
    "2",
  ];

  // 得到两副牌
  const arr = line.split("-");
  let [x, y] = [arr[0], arr[1]];

  if (
    (x.indexOf("j") !== -1 && x.indexOf("JO") !== -1) ||
    (y.indexOf("j") !== -1 && y.indexOf("JO") !== -1)
  ) {
    // 如果有副牌中有大小王,则直接输出大小王
    console.log("joker JOKER");
  } else if (x.indexOf("JO") !== -1 || y.indexOf("JO") !== -1) {
    // 如果有副牌中有大小王,则直接输出大小王
    console.log("JOKER");
  } else if (
    (x.indexOf("j") !== -1 && y.indexOf("JO") === -1) ||
    (y.indexOf("j") !== -1 && x.indexOf("JO") === -1)
  ) {
    // 如果有副牌中有大小王,则直接输出大小王
    console.log("joker");
  } else {
    // 将每张牌中间的空格去除,以方便获取牌的个数
    // 将牌面 10 换成 T -> 因为当类型相同时,需要比较第一个字符确定牌面大小,如果是 10 的话,就会取到 1
    let xTemp = x.replace(/10/g, "T").replace(/ /g, "");
    let yTemp = y.replace(/10/g, "T").replace(/ /g, "");

    // 得到两副牌的个数
    let [xTempLen, yTempLen] = [xTemp.length, yTemp.length];

    if (xTempLen !== yTempLen) {
      // 两副牌个数不相同
      if (xTempLen === 4) {
        // 第一副牌是炸弹,输出第一副牌
        console.log(x);
      } else if (yTempLen === 4) {
        // 第二副牌是炸弹,输出第二副牌
        console.log(y);
      } else {
        // 两副牌都没炸弹,类型不同,无法比较
        console.log("ERROR");
      }
    } else {
      // 两副牌个数相同 -> 类型相同
      if (compare.indexOf(xTemp[0]) > compare.indexOf(yTemp[0])) {
        // 第一副牌的牌面大
        console.log(x);
      } else {
        console.log(y);
      }
    }
  }
}

// HJ89 24点运算
// 4张扑克牌 空格隔开,大小王ERROR
// 1 - 13
// 没有括号
// 顺序任意
// 运算从左往右
// 无解输出NONE
let line = null;
while ((line = readline())) {
  if (line.length > 9) {
    print("ERROR");
    continue;
  }

  let nums = line.split(" ").map((a) => {
    if (a == "A") {
      return 1;
    } else if (a == "J") {
      return 11;
    } else if (a == "Q") {
      return 12;
    } else if (a == "K") {
      return 13;
    } else {
      return parseInt(a);
    }
  });

  let ns = getNums(nums);
  for (let i = 0; i < ns.length; i++) {
    let r = calc24(null, ns[i], ["+", "-", "*", "/"]);
    if (r) {
      print(getS(ns[i][0]) + r);
      break;
    } else if (i == ns.length - 1) {
      print("NONE");
    }
  }
}

function getNums(numbers) {
  let result = [];
  for (let i = 0; i < numbers.length; i++) {
    let num = numbers[i];
    let nums = numbers.slice();
    nums.splice(i, 1);
    //console.log('===' + num + 'dd' + nums + 'kk' + numbers);
    let item = [num];
    if (nums.length > 0) {
      let r = getNums(nums);
      for (let j = 0; j < r.length; j++) {
        result.push(item.concat(r[j]));
      }
      //result.push(item.concat(getNums(nums)));
    } else {
      result.push(item);
    }
  }

  return result;
}

function calc24(result, numbers, symbols) {
  let nums = numbers.slice();
  if (result == null) {
    result = nums.shift();
  }

  let num = nums.shift();
  //console.log(num, 'tt', nums, symbols)
  //print('ddd' + result + 'tt' + num + 'ss' + nums)

  //print('ccrr', result, 'nums', numbers, 'ss', symbols)
  if (result)
    for (let i = 0; i < symbols.length; i++) {
      let s = symbols[i];
      let cr = result;
      if (s == "+") {
        cr = cr + num;
      } else if (s == "-") {
        //print('----' + cr + '----' + num)
        cr = cr - num;
      } else if (s == "*") {
        //print('****' + cr + '****' + num)
        cr = cr * num;
        //print('cr', cr)
      } else if (s == "/") {
        //print('___' + cr + '___' + num)
        cr = Math.floor(cr / num);
      }

      if (nums.length > 0) {
        let r = calc24(cr, nums, symbols); //symbols.filter(a => a != s));
        if (r) {
          return s + getS(num) + r;
        } else {
          continue;
        }
      } else {
        //print('qqqqqqqq', cr)
        if (cr == 24) {
          return s + getS(num);
        } else {
          continue;
        }
      }
    }

  return null;
}

function getS(num) {
  if (num == 1) {
    return "A";
  } else if (num == 11) {
    return "J";
  } else if (num == 12) {
    return "Q";
  } else if (num == 13) {
    return "K";
  } else {
    return num + "";
  }
} // 4张扑克牌 空格隔开,大小王ERROR
// 1 - 13
// 没有括号
// 顺序任意
// 运算从左往右
// 无解输出NONE
let line = null;
while ((line = readline())) {
  if (line.length > 9) {
    print("ERROR");
    continue;
  }

  let nums = line.split(" ").map((a) => {
    if (a == "A") {
      return 1;
    } else if (a == "J") {
      return 11;
    } else if (a == "Q") {
      return 12;
    } else if (a == "K") {
      return 13;
    } else {
      return parseInt(a);
    }
  });

  let ns = getNums(nums);
  for (let i = 0; i < ns.length; i++) {
    let r = calc24(null, ns[i], ["+", "-", "*", "/"]);
    if (r) {
      print(getS(ns[i][0]) + r);
      break;
    } else if (i == ns.length - 1) {
      print("NONE");
    }
  }
}

function getNums(numbers) {
  let result = [];
  for (let i = 0; i < numbers.length; i++) {
    let num = numbers[i];
    let nums = numbers.slice();
    nums.splice(i, 1);
    //console.log('===' + num + 'dd' + nums + 'kk' + numbers);
    let item = [num];
    if (nums.length > 0) {
      let r = getNums(nums);
      for (let j = 0; j < r.length; j++) {
        result.push(item.concat(r[j]));
      }
      //result.push(item.concat(getNums(nums)));
    } else {
      result.push(item);
    }
  }

  return result;
}

function calc24(result, numbers, symbols) {
  let nums = numbers.slice();
  if (result == null) {
    result = nums.shift();
  }

  let num = nums.shift();
  //console.log(num, 'tt', nums, symbols)
  //print('ddd' + result + 'tt' + num + 'ss' + nums)

  //print('ccrr', result, 'nums', numbers, 'ss', symbols)
  if (result)
    for (let i = 0; i < symbols.length; i++) {
      let s = symbols[i];
      let cr = result;
      if (s == "+") {
        cr = cr + num;
      } else if (s == "-") {
        //print('----' + cr + '----' + num)
        cr = cr - num;
      } else if (s == "*") {
        //print('****' + cr + '****' + num)
        cr = cr * num;
        //print('cr', cr)
      } else if (s == "/") {
        //print('___' + cr + '___' + num)
        cr = Math.floor(cr / num);
      }

      if (nums.length > 0) {
        let r = calc24(cr, nums, symbols); //symbols.filter(a => a != s));
        if (r) {
          return s + getS(num) + r;
        } else {
          continue;
        }
      } else {
        //print('qqqqqqqq', cr)
        if (cr == 24) {
          return s + getS(num);
        } else {
          continue;
        }
      }
    }

  return null;
}

function getS(num) {
  if (num == 1) {
    return "A";
  } else if (num == 11) {
    return "J";
  } else if (num == 12) {
    return "Q";
  } else if (num == 13) {
    return "K";
  } else {
    return num + "";
  }
}

// HJ93 数组分组
while ((num = readline())) {
  let nums = readline()
    .trim()
    .split(" ")
    .map((i) => +i);
  let fs = 0,
    ts = 0,
    rs = 0,
    rl = [];
  for (let n of nums) {
    if (n % 5 == 0) {
      fs += n;
    } else if (n % 3 == 0) {
      ts += n;
    } else {
      rs += n;
      rl.push(n);
    }
  }
  let hs = (fs + ts + rs) / 2;
  if (Math.floor(hs) != hs) {
    console.log(false);
  } else {
    console.log(devide(fs, ts, rl, 0));
  }
}
function devide(fs, ts, rl, index) {
  //递归
  if (index == rl.length) {
    return fs == ts;
  }
  return (
    devide(fs + rl[index], ts, rl, index + 1) ||
    devide(fs, ts + rl[index], rl, index + 1)
  );
}

//   HJ95 人民币转换
while ((line = readline())) {
  let number = line.split(".");
  let str = {
    0: "零",
    1: "壹",
    2: "贰",
    3: "叁",
    4: "肆",
    5: "伍",
    6: "陆",
    7: "柒",
    8: "捌",
    9: "玖",
  };
  let [zhengshu, xiaoshu] = number;
  let out = "";
  let temp = "";
  for (let i = zhengshu.length - 1; i >= 0; i--) {
    temp = zhengshu[i] + temp;
    if (temp.length == 4 || i == 0) {
      let s = "";
      if (zhengshu.length - i > 4 && zhengshu.length - i <= 8) {
        s = "万";
      } else if (zhengshu.length - i > 8 && zhengshu.length - i <= 12) {
        s = "亿";
      }
      out = get_four(temp) + s + out;
      temp = "";
    }
  }
  out = out.replace(/^0+/, "");
  out = out.replace(/0+$/, "");
  out = out.replace(/0+/g, "零");
  out = out.replace(/壹拾/g, "拾");
  out = out.replace(/零万/g, "万");
  out = "人民币" + (out == "" ? "" : out + "元") + get_two(xiaoshu);
  console.log(out);
  function get_four(num) {
    let out = "";
    num = num.padStart(4, "0");
    out =
      (num[0] == "0" ? "0" : str[num[0]] + "仟") +
      (num[1] == "0" ? "0" : str[num[1]] + "佰") +
      (num[2] == "0" ? "0" : str[num[2]] + "拾") +
      (num[3] == "0" ? "0" : str[num[3]]);
    return out;
  }
  function get_two(num) {
    if (num[0] == "0" && num[1] == "0") {
      return "整";
    } else {
      return (
        (num[0] == "0" ? "" : str[num[0]] + "角") +
        (num[1] == "0" ? "" : str[num[1]] + "分")
      );
    }
  }
}

// HJ105 记负均正II
let str;
let arr = [];

while ((str = readline())) {
  arr.push(Number(str));
}
let arr2 = arr.filter((item) => item >= 0);
let arr3 = arr.filter((item) => item < 0);
let avg = arr2.length > 0 ? arr2.reduce((t, v) => t + v, 0) / arr2.length : 0;

console.log(arr3.length);
console.log(avg.toFixed(1));

你可能感兴趣的:(javascript,javascript,前端,算法,链表)