1️⃣ 1330번: 두 수 비교하기
https://www.acmicpc.net/problem/1330
💻 나의 풀이
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
💻 나의 풀이
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
💻 나의 풀이
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
💻 나의 풀이
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
💻 나의 풀이
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));
}
}
}
- 만약 분이 45보다 작다면, 시간(hour)에서 1을 뺀 후, 남은 분을 60에서 (45 - 분)을 뺀 값으로 설정합니다.
- 이때, 시간이 음수가 되면 23으로 설정하여 자정(0시)으로 돌아가게 합니다.
- 그렇지 않은 경우(분이 45 이상인 경우)에는 분에서 45를 뺀 값을 출력합니다.
6️⃣ 2525번: 오븐 시계
https://www.acmicpc.net/problem/2525
💻 나의 풀이
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
💻 나의 풀이
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값을 업데이트 해주어야 합니다.