티스토리 뷰

백준

조건문

xoo | 수진 2024. 3. 29. 09:35

1️⃣ 1330번: 두 수 비교하기

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

 

1330번: 두 수 비교하기

두 정수 A와 B가 주어졌을 때, A와 B를 비교하는 프로그램을 작성하시오.

www.acmicpc.net

 

💻 나의 풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();
        int b = sc.nextInt();
        
        if(a>b)
            System.out.println(">");
        else if(a<b)
            System.out.println("<");
        else
            System.out.println("==");
            
    }
}

 

기본적인 조건문 문제이므로 어려울건 없고, 입력받을시 A와B는 공백으로 한칸으로 구분되어 있는 것만 주의해줍시다.

 

 


 

2️⃣ 9498번: 시험 성적

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

 

9498번: 시험 성적

시험 점수를 입력받아 90 ~ 100점은 A, 80 ~ 89점은 B, 70 ~ 79점은 C, 60 ~ 69점은 D, 나머지 점수는 F를 출력하는 프로그램을 작성하시오.

www.acmicpc.net

 

💻 나의 풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int A = sc.nextInt();
        
        if(A >= 90)
            System.out.println("A");
        else if (A >= 80)
            System.out.println("B");
        else if (A >= 70)
            System.out.println("C");
        else if (A >= 60)
            System.out.println("D");
        else
            System.out.println("F");
    }
}

 

코드 가독성을 위해 삼항 연사자를 사용하는 방법도 있습니다.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();

        System.out.println((A>=90)?"A": (A>=80)? "B": (A>=70)? "C": (A>=60)? "D": "F");
    }
}

 


 

 

3️⃣ 2753번: 윤년

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

 

2753번: 윤년

연도가 주어졌을 때, 윤년이면 1, 아니면 0을 출력하는 프로그램을 작성하시오. 윤년은 연도가 4의 배수이면서, 100의 배수가 아닐 때 또는 400의 배수일 때이다. 예를 들어, 2012년은 4의 배수이면서

www.acmicpc.net

 

💻 나의 풀이

import java.util.Scanner;

public class Main {;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int year = sc.nextInt();
        
        if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            System.out.println("1");
        } else {
            System.out.println("0");
        }
    }
}

 

윤년의 연도가 4의 배수 이면서 : if(year % 4 == 0) &&

100의 배수가 아닐 때 또는 : if(year % 100 == 0) ||

400의 배수일 때 : if(year % 400 == 0)


 

 

4️⃣ 14681번: 사분면 고르기

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

 

14681번: 사분면 고르기

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

www.acmicpc.net

 

💻 나의 풀이

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 && Y>0) {
            System.out.println("1");
        } else if(Y>0) {
            System.out.println("2");
        } else if(X<0 && Y<0){
            System.out.println("3");
        } else {
            System.out.println("4");
        }
    }
}

 


 

 

5️⃣ 2884번: 알람 시계

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

 

2884번: 알람 시계

상근이는 매일 아침 알람을 듣고 일어난다. 알람을 듣고 바로 일어나면 다행이겠지만, 항상 조금만 더 자려는 마음 때문에 매일 학교를 지각하고 있다. 상근이는 모든 방법을 동원해보았지만,

www.acmicpc.net

 

💻 나의 풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int hour = sc.nextInt();
        int min = sc.nextInt();
        
        // 45분보다 작으면 시간을 -1
        if(min<45) {
            hour--;
            min = 60-(45-min);
            
            // 0보다 작을시 23시로
            if(hour<0) {
                hour = 23;
            }
            System.out.println(hour + " " + min);
        } else {
            System.out.println(hour + " " + (min - 45));
        }
    }
}
  1. 만약 분이 45보다 작다면, 시간(hour)에서 1을 뺀 후, 남은 분을 60에서 (45 - 분)을 뺀 값으로 설정합니다.
    • 이때, 시간이 음수가 되면 23으로 설정하여 자정(0시)으로 돌아가게 합니다.
  2. 그렇지 않은 경우(분이 45 이상인 경우)에는 분에서 45를 뺀 값을 출력합니다.

 


 

 

6️⃣ 2525번: 오븐 시계

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

 

2525번: 오븐 시계

첫째 줄에 종료되는 시각의 시와 분을 공백을 사이에 두고 출력한다. (단, 시는 0부터 23까지의 정수, 분은 0부터 59까지의 정수이다. 디지털 시계는 23시 59분에서 1분이 지나면 0시 0분이 된다.)

www.acmicpc.net

 

💻 나의 풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int h = sc.nextInt();  // 시
        int m = sc.nextInt();  // 분
        int hour = sc.nextInt();  // 요리하는데 필요한 시간
        
        h += hour / 60;  // 요리시간이 60을 넘는다면 60으로 나눈 몫만큼 h에 더한다.
        m += hour % 60;  // 나머지 분을 m에 더한다.
        
        // m이 60 이상이면 h에 한시간을 더하고, m은 60을 뺀다.
        if(m >= 60) {
            h += 1;
            m -= 60;
        }
        
        // h가 24시 이상이면 24를 뺀다. (24시는 0시)
        if(h >= 24) {
            h -= 24;
        }
        
        System.out.println(h + " " + m);
        
    }
}

 

시간과 분을 잘 생각해봐야 합니다.

60으로 나눈 몫과 나머지를 잘 활용해야 합니다.

 


 

 

7️⃣ 2480번: 주사위 세개

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

 

2480번: 주사위 세개

1에서부터 6까지의 눈을 가진 3개의 주사위를 던져서 다음과 같은 규칙에 따라 상금을 받는 게임이 있다. 같은 눈이 3개가 나오면 10,000원+(같은 눈)×1,000원의 상금을 받게 된다. 같은 눈이 2개만

www.acmicpc.net

 

💻 나의 풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int A = sc.nextInt();  // 첫번째 주사위
        int B = sc.nextInt();  // 두번째 주사위
        int C = sc.nextInt();  // 세번째 주사위
        
        if(A == B && A == C) {  // 세 개가 다 같을 경우 10000 + (같은눈) x 1000
            System.out.println(10000 + A * 1000);
        } else if(A == B && A != C || A == C && A != B) {  // 두 개만 같을 경우 1000 + (같은눈) x 100
            System.out.println(1000 + A * 100);
        } else if(B == C && B != A) {  // 두 개만 같을 경우 1000 + (같은눈) x 100
            System.out.println(1000 + B * 100);
        } else {  // 모두 다른 경우 (그중 가장 큰눈) x 100
            int max = A;
            if (max < B) max = B;
            if (max < C) max = C;
            System.out.println(max * 100);
            
        }
    }
}

 

조건문의 조건들을 주의하며 문제의 수식을 그대로 가져다 쓰면 돼서 간단합니다.

마지막에 가장 큰눈을 찾는 경우도 주의하자! 해당 눈이 더 크다면 max값을 업데이트 해주어야 합니다.

 

 

 

'백준' 카테고리의 다른 글

반복문 (1)  (0) 2024.03.30
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함