[BAEKJOON] 조건문 14681번 - 사분면 고르기

밍챠코·2024년 3월 15일
0

BAEKJOON

목록 보기
15/38

📝[14681]

[Java]

1. Scanner 이용

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int x = sc.nextInt();
        int y = sc.nextInt();
        
        if(x>0){
            if(y>0){
                System.out.print("1");
            } else if(y<0){
                System.out.print("4");
            }
        } else if(x<0){
            if(y>0){
                System.out.print("2");
            } else if(y<0){
                System.out.print("3");
            }            
        }
        
        sc.close();
    }
}

2. BufferedReader 이용

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int x = Integer.parseInt(br.readLine());
        int y = Integer.parseInt(br.readLine());
        
        if(x>0){
            if(y>0){
                System.out.print("1");
            } else {
                System.out.print("4");
            }
        } else {
            if(y>0){
                System.out.print("2");
            } else {
                System.out.print("3");
            }            
        }
        
        br.close();
    }

[Javascript]

🔎 평소에는 테스트 케이스 파일이 존재하는 '/dev/stdin' 경로로 readFileSync Path 값을 넘겨주었지만, 해당 문제에서는 fs모듈 런타임 에러가 발생
('/dev/stdin' 경로에 파일이 없거나 권한이 설정되어 있지 않은 것으로 유추)

💡 해결 방법

1. require('fs').readFileSync(0)

→ 표준 파일 설명자 값(standard input)이 0이기 때문에 별도의 파일('/dev/stdin')이 아닌 표준 입력을 받는 경우에는 0이라는 인자값을 넘겨줌!

const [x, y] = require('fs').readFileSync(0).toString().trim().split("\n").map(Number);

if(x>0){
    console.log(y>0 ? "1" : "4");
} else {
    console.log(y>0 ? "2" : "3");
}

2. readline 모듈 사용

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
 
let input = [];
let quadrantVal;
 
rl.on('line', function (line) {
  input.push(parseInt(line));
}).on("close", function () {
  const x = input[0];
  const y = input[1];
 
  if (x > 0) {
    console.log(y > 0 ? "1" : "4");
  } else {
    console.log(y > 0 ? "2" : "3");
  }
 
  process.exit();
});

💡 readline 모듈 : Javascript 내장 모듈로 입력되는 값들을 readline을 통해 한 줄씩 읽어오고 입력과 출력을 따로 분리하여 작성하는 형태로 구성되어 있음

//readline 모듈 불러오기
const readline = require('readline');
//인터페이스 객체 생성하기(for 콘솔에 표준 입출력 처리)
const rl = readline.createInterface({
    input: process.stdin, //standard input에 대한 Readable Stream
    output: process.stdout //standard output에 대한 Writable Stream
});
//입출력 처리하는 코드
    //'line'- 입력받은 값을 한 줄씩 읽어 문자열 타입으로 전달하는 이벤트
rl.on('line', function (line) { //on() 메서드를 활용하여 이벤트와 콜백함수 전달
    //입력값을 처리하는 코드
    rl.close(); //인터페이스를 종료하여 무한히 입력 받는 것을 방지
    //'close' - 더 이상 입력값이 없을 경우에 해당하는 이벤트
}).on('close', function () {
	//입력을 받은 뒤 실행할 코드
    process.exit();//종료문
});

[Python]

x = int(input())
y = int(input())

if x>0 :
    if y>0 : print("1")
    else : print("4")
else :
    if y>0 : print("2")
    else : print("3")

💡 조건(삼항) 연산자 이용

x = int(input())
y = int(input())

if x>0 :
    print("1" if y>0 else "4")
else :
    print("2" if y>0 else "3")

0개의 댓글