
딱 봐도 O(n^2)로 풀면 시간초과가 날 것 같은 최대 수의 개수다. 힌트에서는 내장 함수를 쓰라고 하길래, 이때를 C++에 있는 정렬 함수를 사용하기로 했다. 이때를 위해 C++로 코딩 테스트를 치자 마음 먹었다.
#include <cstdio>
#include <algorithm>
int main(){
int i, n, a[1000000];
scanf("%d", &n);
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
sort(a, a+n);
for(i=0; i<n; i++){
printf("%d\n", a[i]);
}
}
하지만 실행되지 않았다. [Error] 'sort' was not declared in this scope 라는데, algorithm 헤더는 제대로 넣어놔서 뭐가 문제인지 좀 헤맸다.
문제는 C++을 쓰면 거의 반필수적으로 쓰는 이것, using namespace std;을 쓰지 않아서다.
using namespace std;가 뭐길래?std::cout처럼 호출해 사용해야 한다.using namespace std는 표준 라이브러리(standard library) 있는 객체와 변수의 이름을 사용할 수 있다는 것이다.
또 에러 메시지를 봤을 때, [Note] 'std::sort'라는 메세지가 있었는데, 이는 sort 함수가 std에 들어가있다는 것이다. 그래서 std를 선언해주면
#include <cstdio>
#include <algorithm>
using namespace std;
int main(){
int i, n, a[1000000];
scanf("%d", &n);
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
sort(a, a+n);
for(i=0; i<n; i++){
printf("%d\n", a[i]);
}
}
문제없이 실행! 문제 해결!