[Baekjoon 단계별] 2. if문

Baekjoon > 문제 > 단계별로 풀어보기
언어설정 : node.js

2. if문

1. 1330 / 두 수 비교하기

const [a, b] = require('fs').readFileSync('/dev/stdin').toString().split(' ').map(s => Number(s));
console.log(a > b ? ">" : a < b ? "<" : "==")

2. 9498 / 시험 성적

// 1
let a = +(require('fs').readFileSync('/dev/stdin').toString())
if (a >= 90) {
    console.log("A")
} else if (a >= 80) {
    console.log("B")
} else if (a >= 70) {
    console.log("C")
} else if (a >= 60){
    console.log("D")
} else {
    console.log("F")
}

// 2
s =+(require('fs').readFileSync('/dev/stdin')+'')
console.log(89<s?'A':79<s?'B':69<s?'C':59<s?'D':'F')

// 3
console.log('FFFFFFDCBAA'[Math.floor((require('fs').readFileSync('/dev/stdin')+'')/10)])

3. 2753 / 윤년

// 1
let a = +(require('fs').readFileSync('/dev/stdin').toString())
if ((a % 4 == 0 || a % 100 !== 0) || (a % 400 == 0)) {
    console.log('1')    
} else {
    console.log('0')
}

// 2 
const year = +(require('fs').readFileSync('/dev/stdin').toString());
console.log(+!((year % 4 || !(year % 100)) && year % 400));

4. 14681 / 사분면 고르기 (*fs 사용 불가)

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

let input = [];

rl.on('line', function(line) {
    input.push(parseInt(line));
}).on('close', function() {
    const x = input[0];
    const y = input[1];
    
    if(x > 0 && y > 0) {
        console.log('1');
    }
    else if(x < 0 && y > 0) {
        console.log('2');
    }
    else if(x < 0 && y < 0) {
        console.log('3');
    }
    else {
        console.log('4');
    }
    
    process.exit();
});

// 초기 코드
const [a, b] = require('fs').readFileSync('/dev/stdin').toString().split('\n');
const [a, b] = require('fs').readFileSync('/dev/stdin').toString().split('\n').map(s => Number(s));
let answer = 0
if (+a > 0) {
    if (+b > 0) {
        answer = 1
    } else {
        answer = 2
    }
} else {
    if (+b > 0) {
        answer = 4
    } else {
        answer = 3
    }    
}
console.log(answer)

5. 2884 / 알람 시계

// 1
const [a, b] = require('fs').readFileSync('/dev/stdin').toString().split(' ').map(s => Number(s));
let mm = b-45
let hh = a;
if (mm < 0) {
  mm = mm + 60
  hh = a-1
  if (hh < 0) hh = 23
}
console.log(hh, mm)

// 2
const [h, m] = require('fs').readFileSync(0, 'utf-8').split(' ').map(Number)

let min = h * 60 + m - 45
if(min < 0) min += 1440

console.log(Math.floor(min / 60))
console.log(min % 60)

좋은 웹페이지 즐겨찾기