코딩테스트/백준

(조건문) 사분면 고르기

leehyeon-dv 2024. 12. 29. 19:59

📌점의 좌표를 입력받아 그 점이 어느 사분면에 속하는지 알아내는 프로그램을 작성하시오.

단, x좌표와 y좌표는 모두 양수나 음수라고 가정한다.

 

🔻입출력

 

🔓알고리즘

  • 구현
  • 기하학

 

🐍파이썬

x = int(input())
y = int(input())
if((-1000<=x<=1000 and x != 0) and (-1000<=y<=1000 and y!=0)):
    if(x>0):
        if(y>0):
            print("1")
        else:
            print("4")
    else:
        if (y>0):
            print("2")
        else:
            print("3")

else:
    print("다시 입력")

 

🧩자바

import java.util.Scanner;
public class 사분면 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int x = sc.nextInt();
        int y = sc.nextInt();

        if((-1000<=x && x<=1000 && x != 0) && (-1000<=y && y<=1000 && y!=0)){
            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");
            }
        }
        else{
            System.out.println("다시 입력");
        }

    }
}

 

 

728x90