![[백준 / BOJ] 3003번 킹, 퀸, 룩, 비숍, 나이트, 폰 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FevcKFv%2FbtrLihJcSEl%2F1nVc02wL9Sgf8FB8T2lJvk%2Fimg.jpg)
[백준 / BOJ] 3003번 킹, 퀸, 룩, 비숍, 나이트, 폰 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2022. 9. 3. 22:02
반응형
링크 : https://www.acmicpc.net/problem/3003
3003번: 킹, 퀸, 룩, 비숍, 나이트, 폰
첫째 줄에 동혁이가 찾은 흰색 킹, 퀸, 룩, 비숍, 나이트, 폰의 개수가 주어진다. 이 값은 0보다 크거나 같고 10보다 작거나 같은 정수이다.
www.acmicpc.net
문제

문제 풀이
for문을 활용하는 간단한 문제다.
C++ 상세 풀이
C++ 풀이 펼쳐보기
vector을 사용했다. 아래 코드처럼 입력받자마자 바로 출력해도 정답처리된다.
for (int i = 0; i < 6; ++i) {
std::cin >> var[i];
std::cout << chess[i] - var[i] << " ";
}
Python 상세 풀이
Python 풀이 펼쳐보기
enumerate을 활용해 코드를 짧게 만들어보았다.
for i, v in enumerate(map(int, input().split())):
print(chess[i] - v, end=" ")
코드 전문
C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
void init() { | |
std::ios_base::sync_with_stdio(false); | |
std::cin.tie(nullptr); | |
std::cout.tie(nullptr); | |
} | |
int main() { | |
init(); | |
int chess[6] = {1, 1, 2, 2, 2, 8}; | |
std::vector<int> var(6); | |
for (int i = 0; i < 6; ++i) { | |
std::cin >> var[i]; | |
std::cout << chess[i] - var[i] << " "; | |
} | |
return 0; | |
} |
Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
input = sys.stdin.readline | |
if __name__ == "__main__": | |
chess = [1, 1, 2, 2, 2, 8] | |
for i, v in enumerate(map(int, input().split())): | |
print(chess[i] - v, end=" ") |
소감
enumerate를 잘 활용하면 굳이 for i in range()문을 사용하지 않아도 된다.
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 2588번 곱셈 (C++, Python) (0) | 2022.09.04 |
---|---|
[백준 / BOJ] 10430번 나머지 (C++, Python) (0) | 2022.09.04 |
[백준 / BOJ] 18108번 1998년생인 내가 태국에서는 2541년생?! (C++, Python) (0) | 2022.09.02 |
[백준 / BOJ] 10926번 ??! (C++, Python) (0) | 2022.09.01 |
[백준 / BOJ] 10869번 사칙연산 (C++, Python) (0) | 2022.09.01 |
@Reo :: 코드 아카이브
자기계발 블로그