0
0

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 1 year has passed since last update.

AtCoderのインタラクティブ問題をjavascriptで提出する

Posted at

AtCoderのインタラクティブ問題をjavascriptで提出する

結論だけ言うと、
入力はnodejsのreadlineモジュールを使う
出力はconsole.log()でOK(flush処理は特にしない)
最後にreadlineをcloseしておいた方がよい(下記の問題では不要だったがAHC008では必要だった)

例として
https://atcoder.jp/contests/language-test-202001/tasks/practice_2
にあるC++のサンプルコードを移植してみると:

l_interactive_sorting.ts
import {createInterface} from 'readline';

const readline = createInterface({
  input: process.stdin,
  // output: process.stdout,
});

const input: string[] = [];

readline.on('line', (line: string) => {
  input.push(line);
});

const getLine = async () => {
  while (input.length === 0) {
    await new Promise(resolve => setTimeout(resolve, 0));
  }
  return input.shift();
};

const close = () => {
  readline.close();
};

const main = async () => {
  const [N, Q] = (await getLine()).split(' ').map(elem => parseInt(elem));

  const s = [];
  for (let i = 0; i < N; ++i) {
    s.push(String.fromCharCode('A'.charCodeAt(0) + i));
  }

  for (let i = 0; i < N; ++i) {
    for (let j = 0; j < N - 1; ++j) {
      console.log(`? ${s[j]} ${s[j + 1]}`);
      const ans = await getLine();
      if (ans === '>') {
        const work = s[j];
        s[j] = s[j + 1];
        s[j + 1] = work;
      }
    }
  }
  console.log(`! ${s.join('')}`);

  close();
};

main();
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?