Java/알고리즘 공부
[백준 JAVA] 3003번: 킹, 퀸, 룩, 비숍, 나이트, 폰
O'mil
2022. 12. 19. 21:38
728x90
문제
풀이
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int A = in.nextInt();
int B = in.nextInt();
int C = in.nextInt();
int D = in.nextInt();
int E = in.nextInt();
int F = in.nextInt();
if (A == 1) {
System.out.print("0 ");
} else {
A = 1 - A;
System.out.print(A + " ");
}
if (B == 1) {
System.out.print("0 ");
} else {
B = 1 - B;
System.out.print(B + " ");
}
if (C == 2) {
System.out.print("0 ");
} else {
A = 2 - C;
System.out.print(C + " ");
}
if (D == 2) {
System.out.print("0 ");
} else {
D = 2 - D;
System.out.print(D + " ");
}
if (E == 2) {
System.out.print("0 ");
} else {
E = 2 - E;
System.out.print(E + " ");
}
if (F == 8) {
System.out.print("0 ");
} else {
F = 8 - F;
System.out.print(F + " ");
}
in.close();
}
}
- 처음 나는 조건문으로 접근을 했다.
( 아직 조건문이 나올 단계가 아니라 위 코드가 정답이 아닐거라고 생각했지만 떠오르는 풀이가 저것 뿐이었다.)
- 출력결과는 제대로 나왔지만 제출을 했을 때는 오답이라고 떴다.
- 구글에 풀이를 검색해 보았다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int K = 1;
int Q = 1;
int R = 2;
int B = 2;
int Kn = 2;
int P = 8;
K = K - in.nextInt();
Q = Q - in.nextInt();
R = R - in.nextInt();
B = B - in.nextInt();
Kn = Kn - in.nextInt();
P = P - in.nextInt();
System.out.print(K + " ");
System.out.print(Q + " ");
System.out.print(R + " ");
System.out.print(B + " ");
System.out.print(Kn + " ");
System.out.print(P + " ");
in.close();
}
}
- 각 피스의 개수에서 동혁이가 찾은 피스의 개수를 빼면 되므로
- 우선 각 피스를 정수형으로 선언하고 입력받은 값을 뺀 후 출력을 해주는 방식이다.
- 복잡하게 if문을 쓸 필요없이 선언을 해줄 때 부터 빼기를 해주면 되는 것이었다.
참고)
https://st-lab.tistory.com/297
728x90