https://www.acmicpc.net/problem/10867
코드
#include <iostream>
#include <set>
using namespace std;
int main(){
int n;
cin >> n;
set<int> s;
int tmp;
for(int i=0;i<n;i++){
scanf("%d", &tmp);
s.insert(tmp);
}
for(set<int>::iterator iter=s.begin();iter!=s.end();iter++){
printf("%d ", *iter);
}
return 0;
}
이 문제는 집합을 이용하여 풀면 쉽게 풀 수 있다. 다행히 C++에는 집합이 이미 구현되어 있기 때문에 가져다 쓸 수 있다.
#include <set>
집합에 입력받은 수들을 넣었다가 출력해주기만 하면 되는데, set은 기본적으로 원소를 넣을 때마다 정렬되어서 삽입되므로 정렬해줄 필요가 없다.
출력할 때는 set은 배열처럼 인덱스로 접근이 불가하다. 그렇기 때문에 아래와 같이 iterator로 접근해야 한다.
for(set<int>::iterator iter=s.begin();iter!=s.end();iter++){
printf("%d ", *iter);
}
'문제풀이 > C, C++' 카테고리의 다른 글
[C언어] 백준 6588번 골드바흐의 추측 (0) | 2024.04.24 |
---|---|
[C언어] 백준 14916번 거스름돈 (0) | 2023.11.20 |
[C언어] 백준 10828번 스택 (0) | 2023.11.17 |
[C언어] 백준 1094번 막대기 (0) | 2023.11.16 |
[C언어] 백준 2302번 극장 좌석 (0) | 2023.11.16 |