반응형
Preface
2단계는 조건문과 관련된 문제들이다.
나는 2884번을 풀 때 절댓값을 사용해서 출력값을 얻었지만, 다른 사람들의 코드를 보니 내가 작성한 코드보다 훨씬 간단했다.
또, 2525번 문제는 내가 작성한 코드를 제출하면 오답으로 인식되어 어쩔 수 없이 다른 코드를 제출했다.
분명 출력값이 주어진 조건과 동일하게 나오는데 왜 오답으로 처리되는지 잘 모르겠다.
두 문제 뿐만이 아니라 다른 몇몇 문제들도 다른 사람들의 코드에 비해 내 코드가 훨씬 길고 복잡했다.
물론 문제 해결 방법이 특정되어 있는 것은 아니지만, 아무래도 내 코드는 효율성이 많이 떨어지는 것 같다.
나는 왜 대다수의 사람들처럼 간결한 코드를 구성하지 못할까.
앞으로 꾸준히 다양한 문제를 접하다 보면 적절한 알고리즘을 선택할 수 있는 능력을 기를 수 있으려나..
https://www.acmicpc.net/step/4
- 1330번: 두 수 비교하기
package num2;
import java.util.Scanner;
public class B1330 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int A = scanner.nextInt();
int B = scanner.nextInt();
if (A > B) {
System.out.println(">");
} else if (A < B) {
System.out.println("<");
} else {
System.out.println("==");
}
scanner.close();
}
}
- 9498번: 시험 성적
package num2;
import java.util.Scanner;
public class B9498 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int A = scanner.nextInt();
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");
}
scanner.close();
}
}
- 2753번: 윤년
package num2;
import java.util.Scanner;
public class B2753 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int A = scanner.nextInt();
if ((A % 4 == 0) && (A % 100 != 0) || (A % 400 == 0)) {
System.out.println(1);
} else {
System.out.println(0);
}
scanner.close();
}
}
- 14681번: 사분면 고르기
package num2;
import java.util.Scanner;
public class B14681 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int A = scanner.nextInt();
int B = scanner.nextInt();
if (A > 0 && B > 0) {
System.out.println(1);
} else if (A < 0 && B > 0) {
System.out.println(2);
} else if (A < 0 && B < 0) {
System.out.println(3);
} else {
System.out.println(4);
}
scanner.close();
}
}
- 2884번: 알람 시계
package num2;
import java.util.Scanner;
public class B2884 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int H = scanner.nextInt();
int M = scanner.nextInt();
if (M >= 45) {
System.out.print(H + " " + (M - 45));
} else if (M < 45) {
if (H - 1 < 0) {
H = 23;
System.out.print(H + " ");
} else {
System.out.print((H - 1) + " ");
}
System.out.print(60 - Math.abs(M - 45));
}
scanner.close();
}
}
// H 값은 0~23, M 값은 0~59로 설정한다.
// 45분을 뺐을 때 M 값이 음수가 되면 H 값을 1 감소시킨 후 M 값은 59에서부터 뺀다.
// H 값을 감소시켰을 때 음수가 되면 23이 되어야 한다.
- 2525번(틀린 정답): 오븐 시계
package num2;
import java.util.Scanner;
public class B2525 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int A = scanner.nextInt();
int B = scanner.nextInt();
int C = scanner.nextInt();
scanner.close();
if (B + C > 59) {
A = A + ((B + C) / 60);
if (A > 23) {
A = ((B + C) / 60 - 1);
}
System.out.println(A + " " + ((B + C) % 60));
} else {
System.out.println(A + " " + (B + C));
}
}
}
// A가 23 이상이면 0부터 시작
// B + C가 60 이상이면 합을 60으로 나눈 몫을 A에 더해야 함
// 그 이후 B+C를 60으로 나눈 나머지를 분으로 출력해야 함
- 2525 다른 해설: 오븐 시계
package num2;
import java.util.Scanner;
public class B2525_A {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int A = in.nextInt();
int B = in.nextInt();
int C = in.nextInt();
in.close();
int min = 60 * A + B; // 시 -> 분
min += C;
int hour = (min / 60) % 24;
int minute = min % 60;
System.out.println(hour + " " + minute);
}
}
→ 출처: https://st-lab.tistory.com/292
- 2480번: 주사위 세개
package num2;
import java.util.Scanner;
public class B2480 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int A = scanner.nextInt();
int B = scanner.nextInt();
int C = scanner.nextInt();
scanner.close();
int[] sumArray = { A, B, C };
int max = sumArray[0];
int sum;
for (int i = 0; i < sumArray.length; i++) {
if (sumArray[i] > max) {
max = sumArray[i];
}
}
if (A == B && B == C) {
sum = 10000 + (A * 1000);
System.out.println(sum);
} else if (A == B || B == C || C == A) {
if (A == B) {
sum = 1000 + (A * 100);
System.out.println(sum);
} else if (B == C) {
sum = 1000 + (B * 100);
System.out.println(sum);
} else if (C == A) {
sum = 1000 + (C * 100);
System.out.println(sum);
}
} else {
sum = max * 100;
System.out.println(sum);
}
}
}
- 2480 다른 해설: 주사위 세개
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int a, b, c;
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
// 만약 모든 변수가 다른 경우
if (a != b && b != c && a != c) {
int max = Math.max(a, Math.max(b, c));
System.out.println(max * 100);
}
// 3개의 변수가 모두 같은 경우
else if (a == b && a == c) {
System.out.println(10000 + a * 1000);
}
// a가 b혹은 c랑만 같은 경우
else if(a == b || a == c) {
System.out.println(1000 + a * 100);
}
// b가 c랑 같은 경우
else {
System.out.println(1000 + b * 100);
}
}
}
→ 출처: https://st-lab.tistory.com/295
728x90
반응형
'Algorithm > 백준(BOJ)' 카테고리의 다른 글
백준 6단계 - Java (0) | 2023.05.22 |
---|---|
백준 5단계 -Java (2) | 2023.04.18 |
백준 4단계 - Java (0) | 2023.04.11 |
백준 3단계 - Java (0) | 2023.04.02 |
백준 1단계 - Java (0) | 2023.03.22 |
댓글