알고리즘 문제풀이/Beakjoon

[자바/백준] 단계별 문제 풀이 - if문

joah.k 2021. 6. 19. 21:50
728x90
1 1330번 두 수 비교하기
import java.util.Scanner;

public class step02_1 {
    public static void main(String[] args) {
        //1330번. 두 수 비교하기
        //두 수를 비교한 결과를 출력하는 문제
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        sc.close();

        if(a >= -10000 && b <= 10000){
            if(a>b)
                System.out.println(">");
            else if(a<b)
                System.out.println("<");
            else if(a==b)
                System.out.println("==");
        }else
            System.out.println("error!");
    }
}

 

 

2 9498번 시험 성적
import java.util.Scanner;

public class step02_2 {
    public static void main(String[] args) {
        //9498번. 시험 성적
        //시험 점수를 성적으로 바꾸는 문제
        Scanner sc = new Scanner(System.in);
        int score = sc.nextInt();
        sc.close();

        if(score>=0 && score<=100) {
            if (score >= 90)
                System.out.println("A");
            else if (score >= 80)
                System.out.println("B");
            else if (score >= 70)
                System.out.println("C");
            else if (score >= 60)
                System.out.println("D");
            else
                System.out.println("F");
        }
    }
}

 

 

3 2753번 윤년
import java.util.Scanner;

public class step02_3 {
    public static void main(String[] args) {
        //2753번. 윤년
        //윤년을 판별하는 문제
        Scanner sc = new Scanner(System.in);
        int year = sc.nextInt();
        sc.close();

        if(year>=1 && year<=4000) {
            if (year%4==0 && year%100!=0||year % 400== 0)
                System.out.println("1");
            else
                System.out.println("0");
        }
    }
}
​

 

 

 

4 14681번 사분면 고르기
import java.util.Scanner;

public class step02_4 {
    public static void main(String[] args) {
        // 14681번. 사분면 고르기
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        sc.close();

        if(x>= -1000 && x<=1000 && x!=0) {
            if (y >= -1000 && y <= 1000 && y != 0) {
                if(x>0 && y>0)
                    System.out.println("1");
                else if(x<0 && y>0)
                    System.out.println("2");
                else if(x<0 && y<0)
                    System.out.println("3");
                else if(x>0 && y<0)
                    System.out.println("4");
            }
        }
    }
}
​

 

 

 

5 2884번 알람 시계

* 분(m) 기준으로 조건을 나눌 것 

* 분(m)이 45보다 작을 경우 시(h) 처리 -> ex) 0시 5분 입력 시 , -1시 20분이 되어버리니까 시를 23으로 설정 

import java.util.Scanner;

public class step02_5 {
    public static void main(String[] args) {
        //2884번. 알람 시계
        //45분 일찍 알람 설정하기
        Scanner sc = new Scanner(System.in);
        int h = sc.nextInt();
        int m = sc.nextInt();
        sc.close();
            if(h<=24){
                if(m>=0 && m<=60){
                    if(m<45){
                        h--;
                        m=(m+60)-45;

                        if(h<0) {
                            h = 23;
                        }
                        System.out.println(h + " " +m);
                    }
                    else
                        System.out.println(h + " " + (m-45));
                    }
                }
            }
    }

728x90