알고리즘 문제
[백준] 1920번 수 찾기
feelcoding
2020. 2. 3. 01:11
728x90
1920번: 수 찾기
첫째 줄에 자연수 N(1≤N≤100,000)이 주어진다. 다음 줄에는 N개의 정수 A[1], A[2], …, A[N]이 주어진다. 다음 줄에는 M(1≤M≤100,000)이 주어진다. 다음 줄에는 M개의 수들이 주어지는데, 이 수들이 A안에 존재하는지 알아내면 된다. 모든 정수들의 범위는 int 로 한다.
www.acmicpc.net
비주얼 스튜디오에서 돌릴 때에는 문제 없었지만 제출할 때에는 algorithm 헤더파일을 include 하지 않으니 컴파일 에러가 났다. 오늘도 잊지말자. find() 함수를 쓰려면 <algorithm> 헤더파일을 포함하자.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int n, m;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cin >> m;
for (int i = 0; i < m; i++) {
int temp;
cin >> temp;
vector<int>::iterator iter;
iter = find(arr.begin(), arr.end(), temp);
if (iter != arr.end()) {
cout << 1 << '\n';
}
else cout << 0 << '\n';
}
return 0;
}
앞에 cin.tie(NULL);과 ios_base::sync_with_stdio(false);는 입출력 속도 향상을 위함이다. 이 글을 참고하면 된다.
[C++] 백준 시간초과 문제 해결 입출력 속도 향상
cin과 cout을 사용한다면 cin.tie(NULL); ios_base::sync_with_stdio(false) 이 코드를 추가해주면 속도가 향상된다. 그리고 줄바꿈을 해야 한다면 cout << a << endl; 보다는 cout << a << '\n' 이렇게 해주는 것..
breakcoding.tistory.com
줄바꿈을 할 때에 endl을 쓰지 않고 '\n'을 써준 것도 시간초과가 나지 않도록 하기 위함이다.
728x90