-
[백준] 3003번 : 킹, 퀸, 룩, 비숍, 나이트, 폰[JAVA]Algorithm/백준[JAVA] 2022. 12. 25. 03:07
# 문제
# 접근방식
- 원래 들어있는 킹, 퀸, 룩, 비숍, 나이트, 폰의 개수를 입력한 개수와 비교해야하기 때문에 배열을 이용한다.
- 원래 들어있는 체스들의 개수로 초기화한 chess변수를 선언한다.
- 동혁이가 찾은 체스들의 개수를 입력할 white변수를 선언한다.
- for문을 이용하여 원래 체스와 입력한 체스를 하나씩 비교한다.
- 입력한 체스가 원래 체스의 개수만큼 있다면 출력되는 값은 0이라는 것을 생각한다.
- if문을 이용하여 입력한 체스 개수가 0보다 작은 경우(음수)와 0보다 큰 경우(양수)를 나타낸다.
- 음수인경우는 원래 체스 개수에서 입력한 체스 개수만큼 제거한다.
- 양수인경우는 원래 체스 개수에서 입력한 체스 개수만큼 더해준다.
# 소스코드
# 성능
# 메모
위 소스코드는 처음에 배열에는 scanner로 입력 못할 것이라고 생각해서 check를 입력받을 변수로 선언한 것이다.
배열에 입력한 후 비교하는 배열을 선언함으로써 불필요한 변수check가 없어져서 성능이 좋아졌다.
▶ 불필요한 변수 줄이기.
'Algorithm > 백준[JAVA]' 카테고리의 다른 글
[백준] 10171번 : 고양이[JAVA] (0) 2023.02.12 [백준] 10430번 : 나머지[JAVA] (0) 2023.02.12 [백준] 18108번 : 1998년생인 내가 태국에서는 2541년생?![JAVA] (0) 2022.12.25 [백준] 10926번 : ??![JAVA] (0) 2022.12.25 [백준] 10869번 : 사칙연산[JAVA] (0) 2022.12.25