백준 알고리즘 2단계
📖 백준 알고리즘 2단계
❗ 2단계 풀이와 생각
2022-02-08 백준알고리즘
Q.9898
제출 코드
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
sc.close();
if(a >= 0 && a =< 100) {
if(a >= 90 && a <= 100) {
System.out.println("A");
}else if(a >= 80 && a <= 89) {
System.out.println("B");
}else if(a >= 70 && a <= 79) {
System.out.println("C");
}else if(a >= 60 && a <= 69) {
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();
sc.close();
if(a >= 90 && a <= 100) {
System.out.println("A");
}else if(a >= 80 && a <= 89) {
System.out.println("B");
}else if(a >= 70 && a <= 79) {
System.out.println("C");
}else if(a >= 60 && a <= 69) {
System.out.println("D");
}else {
System.out.println("F");
}
}
}
✏️ 몰랐던 부분 & 틀린 부분
- 입력부분의 시험점수의 조건을 조건문에 넣었지만 이 부분이 오류가 떴다. 정답은 입력부분의 조건문을 빼면 된다.
Q.14681
💡 나의 방법론
- 가장 단순 하게 하나하나 나누기
- 양수는 양수대로 음수는 음수대로 나눠보기
제출 코드 1
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 && 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(4);
}else {
System.out.println(3);
}
}
}
제출 코드 2
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.print(1);
}
else {
System.out.print(4);
}
}
else {
if(y > 0) {
System.out.print(2);
}
else {
System.out.print(3);
}
}
}
}
Q.2884
💡 나의 방법론
- 입력시간에서 45분(Minute)을 뺀다 → 만약 44분(Minute)이라면 음수가 되어버린다
- 분(Minute)이 음수가 된다면 시간(Hour)은 -1 시간(Hour)이 된다.
- 결국 분(Minute)을 중심으로 조건문을 만들어야 한다.
제출 코드
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int h = sc.nextInt(); // 시간(hour)
int m = sc.nextInt(); // 분(minute)
sc.close();
if(m < 45) {
h--; // 시(hour) 1 시간 감소
m+=15; // 60 - (45 - m); // 분(Minute) 감소
if(h < 0) {
h = 23; // 0시 보다 1시간 앞선건 무조건 23시
}
System.out.println(h + " " + m);
}
else {
System.out.println(h + " " + (m - 45));
}
}
}
✏️ 몰랐던 부분 & 틀린 부분
- 패키지를 입력한 상태로 제출 하여 오답판정을 받음
Leave a comment