1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Twitterの面白そうな問題解いてみた

Posted at

◇    ◇    ◇
ーー +  ーー + ーー = 1
◇◇   ◇◇   ◇◇

上の式が成り立つように◇に1から9までの数字を1個ずつ入れてください。数字の重複は不可とする。

※解説必要でしたら後で追記します。

let num: number[] = [0, 0, 0, 0, 0, 0, 0, 0, 0];
let used: boolean[] = [
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false
];

gen(1);

function gen(in_num: any) {
  if (in_num > 9) {
    if (
      calculate(
        num[0],
        num[1],
        num[2],
        num[3],
        num[4],
        num[5],
        num[6],
        num[7],
        num[8]
      ) === 1
    ) {
      console.log(
        num[0] +
          " / (" +
          num[1] +
          num[2] +
          ") + " +
          num[3] +
          " / (" +
          num[4] +
          num[5] +
          ") + " +
          num[6] +
          " / (" +
          num[7] +
          num[8] +
          ") = 1"
      );
    }
  } else {
    for (let cnt = 0; cnt <= 9; cnt++) {
      if (used[cnt] == false) {
        used[cnt] = true;
        num[cnt] = in_num;
        gen(in_num + 1);
        used[cnt] = false;
      }
    }
  }
}

function calculate(
  parm1: number,
  parm2: number,
  parm3: number,
  parm4: number,
  parm5: number,
  parm6: number,
  parm7: number,
  parm8: number,
  parm9: number
): number {
  return (
    parm1 / (parm2 * 10 + parm3) +
    parm4 / (parm5 * 10 + parm6) +
    parm7 / (parm8 * 10 + parm9)
  );
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?