2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

少し前から自己研鑽のためにLeetCodeという競技プログラミングの問題を解いています。
paiza×Qiita記事投稿キャンペーンを機にpaizaの問題を解いていこうと思い、コードを公開します。

前回はDランクの問題だったので、今回はCランクの問題です。
言語はJavaScript(TypeScriptがなかった...)を選択しています。

Dランクはこちら

宝くじ

process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
  const isAdjacent = (winNumber, num) => {
    if ((winNumber !== 100000) && (num === winNumber-1)) {
      return true;
    } else if ((winNumber !== 199999) && (num === winNumber+1)) {
      return true;
    } else {
      return false;
    }
  };
  
  const isSecond = (winNumber, num) => {
    return (winNumber % 10000) === (num % 10000);
  };
  
  const isThird = (winNumber, num) => {
      return (winNumber % 1000) === (num % 1000);
  };
  
  const winNumber = Number(lines[0]);
  
  for(let i=2; i<Number(lines[1])+2; i++){
      const num = Number(lines[i]);
      
      if (num === winNumber) {
          console.log('first');
      } else if (isAdjacent(winNumber, num)) {
          console.log('adjacent');
      } else if (isSecond(winNumber, num)) {
          console.log('second');
      } else if (isThird(winNumber, num)) {
          console.log('third');
      } else {
          console.log('blank');
      }
  }
});

野球の審判

process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
  let strkes = 0;
  let balls = 0;
  for (let i=1; i<Number(lines[0])+1; i++) {
      const judge = lines[i];
      if (judge === 'strike') {
          if (strkes === 2) {
            console.log('out!');
          } else {
            console.log('strike!');
            strkes++;
          }
      } else {
          if (balls === 3) {
            console.log('fourball!');
          } else {
            console.log('ball!');
            balls++;
          }
      }
  }
});

みかんの仕分け

process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
  const NM = lines[0].split(' ');
  const N = Number(NM[0]);
  const M = Number(NM[1]);
  
  for (let m=1; m<M+1; m++) {
    const w = lines[m];
    const addIndex = ((N / 2) <= (w % N)) ? 1 : 0;
    const index = Math.floor(w / N);
    
    let box;
    if (index === 0) {
      box = N;
    } else {
      box = (index + addIndex) * N;
    }
    console.log(box);
  }
});

Fizz Buzz

process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
  for (let i=1; i<=Number(lines[0]); i++) {
    if ((i % 15) === 0) {
      console.log('Fizz Buzz');
    } else if ((i % 3) === 0) {
      console.log('Fizz');
    } else if ((i % 5) === 0) {
      console.log('Buzz');
    } else {
      console.log(i);
    }
  }
});

残り物の量

process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
  const mpq = lines[0].split(' ');
  const m = Number(mpq[0]);
  const p = Number(mpq[1]);
  const q = Number(mpq[2]);
  
  const freshed = m * p / 100;
  const dished = (m - freshed) * q / 100;
  
  console.log(m - freshed - dished);
});

さいごに

Cランクもそこまで複雑な問題はない印象です。
Dランクの次ステップとしていい感じの難易度かなと感じました。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?