코딩테스트/백준(JAVA)

백준 코딩테스트 2-4-14681 사분면 고르기 코딩 문제 java로 푸는 방법

쏠솔랄라 2023. 3. 9. 11:53

 

 

백준 코딩테스트 단계별로 풀어보기

2단계 조건문 4단계 14681 사분면 고르기 문제입니다

 

 

https://www.acmicpc.net/problem/14681

 

14681번: 사분면 고르기

점 (x, y)의 사분면 번호(1, 2, 3, 4 중 하나)를 출력한다.

www.acmicpc.net

 

https://www.acmicpc.net/step

 

단계별로 풀어보기

단계별은 @jh05013님이 관리하고 계십니다. 단계제목설명정보총 문제내가 맞은 문제1입출력과 사칙연산입력, 출력과 사칙연산을 연습해 봅시다. Hello World!132조건문if 등의 조건문을 사용해 봅시다

www.acmicpc.net

 

 


 

 

 

 

문제
흔한 수학 문제 중 하나는 주어진 점이 어느 사분면에 속하는지 알아내는 것이다.
사분면은 아래 그림처럼 1부터 4까지 번호를 갖는다. "Quadrant n"은 "제n사분면"이라는 뜻이다.예를 들어, 좌표가 (12, 5)인 점 A는 x좌표와 y좌표가 모두 양수이므로 제1사분면에 속한다. 점 B는 x좌표가 음수이고 y좌표가 양수이므로 제2사분면에 속한다.점의 좌표를 입력받아 그 점이 어느 사분면에 속하는지 알아내는 프로그램을 작성하시오.
단, x좌표와 y좌표는 모두 양수나 음수라고 가정한다.

입력
첫 줄에는 정수 x가 주어진다. (−1000 ≤ x ≤ 1000; x ≠ 0)
다음 줄에는 정수 y가 주어진다. (−1000 ≤ y ≤ 1000; y ≠ 0)

출력
점 (x, y)의 사분면 번호(1, 2, 3, 4 중 하나)를 출력한다.

 

정답

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

sc.close();

 

if ( x > 0 ) {

if (y > 0) {

System.out.println("1");

} else {

System.out.println("4");

}

} else {

if (y > 0) {

System.out.println("2");

} else {

System.out.println("3");

}

}

}

}

 

=> 4개 영역 각각 해당하는 조건을 if문으로 구현

 

 


 


1-1-2557 Hello World https://developernew.tistory.com/41 <- 백준 답지 제출 방법
1-2-1000 A+B https://developernew.tistory.com/42
1-3-1001 A-B https://developernew.tistory.com/43
1-4-10998 AXB https://developernew.tistory.com/44
1-5-1008 A/B https://developernew.tistory.com/45
1-6-10869 사칙연산 https://developernew.tistory.com/46
1-7-10926 ??! https://developernew.tistory.com/47
1-8-18108 1998년생인 내가 태국에서는 2541년생?! https://developernew.tistory.com/48
1-9-10430 나머지 https://developernew.tistory.com/49
1-10-2588 곱셈 https://developernew.tistory.com/50
1-11-11382 꼬마 정민 https://developernew.tistory.com/51
1-12-10171 고양이 https://developernew.tistory.com/52
1-13-10172 개 https://developernew.tistory.com/53

2-1-1330 두 수 비교하기 https://developernew.tistory.com/54
2-2-9498 시험 성적 https://developernew.tistory.com/55
2-3-2753 윤년 https://developernew.tistory.com/56