백준 14681 자바(JAVA) 사분면 - BufferedReader 입력
백준 14681 사분면 문제에 대해 입력방식을 BufferedReader와 Scanner를 사용하여 채점결과에 대한 메모리와 시간을 비교하여 풀어보았다.
문제
입력 / 출력 / 예제
채점 결과
아래의 결과는 Scanner 입력방식이고 위의 결과는 BufferedReader 입력 방식이다.
확실히 메모리/시간 부분에 있어서 Scanner입력 방식보다 BufferedReader 입력방식이 우수한 것을 볼 수 있다.
Scanner 입력방식
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int result = 0;
if (x < -1000 || x > 1000 || x == 0 || y < -1000 || y > 1000 || y == 0)
{
return;
}
if (x > 0 && y > 0)
{
result = 1;
}
else if (x < 0 && y > 0)
{
result = 2;
}
else if (x < 0 && y < 0)
{
result = 3;
}
else if (x > 0 && y < 0)
{
result = 4;
}
else
{
return;
}
System.out.println(result);
}
}
BufferedReader 입력방식
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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());
int result = 0;
if (x < -1000 || x > 1000 || x == 0 || y < -1000 || y > 1000 || y == 0)
{
return;
}
if (x > 0 && y > 0)
{
result = 1;
}
else if (x < 0 && y > 0)
{
result = 2;
}
else if (x < 0 && y < 0)
{
result = 3;
}
else if (x > 0 && y < 0)
{
result = 4;
}
else
{
return;
}
System.out.println(result);
}
}
'자바(JAVA) > 자료구조 & 알고리즘' 카테고리의 다른 글
백준 2480 자바(JAVA) 주사위 세개 (0) | 2023.05.06 |
---|---|
백준 2525번 자바(JAVA) 오븐 시계 (0) | 2023.05.06 |
백준 2884번 자바(JAVA) 알람시계 (0) | 2023.05.05 |
백준 2753 JAVA(자바) 윤년 (0) | 2023.04.30 |
백준 9498번 JAVA(자바) 시험성적 (0) | 2023.04.30 |