BOJ | 10871번

송치헌·2021년 6월 12일
0
post-thumbnail

Python 풀이

N, X = map(int,input().split())
A = list(map(int,input().split())) #int형 변수를 가지는 map을 list로 받아옴
for i in A:
    if i < X:
        print(i,end=' ')

변수 A를 list로 만들어서 입력을 split()으로 잘라 리스트의 각 인덱스로 매핑한다.
그렇게 리스트를 만들어 반복문을 돌려서 리스트의 각 원소가 X보다 작으면 출력해준다.

파이썬에서는 좀 더 쉽게 코딩할 수도 있다.

N, X = map(int,input().split())
A = [*map(int,input().split())]
print(*[i for i in A if i < X])

A를 map의 주소값을 담은 리스트로 저장하고 출력할 때 그 주소값을 참조하여 출력해준다. i for i in A if i < Xi를 출력하는데 iA를 순회하며 i < X일 때만 출력해 준다는 뜻이다.

C++ 풀이

#include <iostream>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, x;
    int a;
    cin >> n >> x;

    while (n--) {
    	cin >> a;
    	if (a < x)cout << a << '\n';
    }
}
profile
https://oraange.tistory.com/ 여기에도 많이 놀러와 주세요

0개의 댓글