백준 14681번
<if - else>
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);
}
}
<삼항연산자>
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();
System.out.println((x > 0) ? ((y > 0) ? 1 : 4) : ((y > 0) ? 2 : 3));
}
}
백준 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();
sc.close();
if (hour != 0) {
if (min >= 45)
min -= 45;
else if (min < 45 && min > 30) {
hour -= 1;
min += 15; // min = 60 - (45 - min);
}
else if (min <= 30) {
hour -= 1;
min += 15; // min = 60 - (45 - min);
}
}
else if (hour == 0) {
if (min >= 45)
min -= 45;
else if (min < 45 && min > 30) {
hour = 23;
min += 15; // min = 60 - (45 - min);
}
else if (min <= 30) {
hour = 23;
min += 15; // min = 60 - (45 - min);
}
}
System.out.println(hour + " " + min);
}
}
백준 문제 추가로 2문제 더 풀어봤습니다.(14681번, 2884번)
감사합니다!
'JAVA' 카테고리의 다른 글
for 반복문 (0) | 2021.06.30 |
---|---|
while /do - while 반복문 (0) | 2021.06.29 |
if / if - else 조건문 (0) | 2021.06.28 |
여러 가지 연산자 (0) | 2021.06.26 |
상수 및 형변환 (0) | 2021.06.25 |