2751번: 수 정렬하기 2


#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long N = 0;
long long numArr[1000001] = {};
cin >> N;
for (long long i = 0; i < N; i++)
{
cin >> numArr[i];
}
sort(numArr, numArr + N);
for (long long i = 0; i < N; i++)
{
cout << numArr[i]<<"\n";
}
}
메모리 (KB) | 시간 (ms) | 코드 길이 (B) |
9712 | 280 | 350 |
🙋 더 알아보기
🤔 C++ 문제인데 C로 푸는 이유가 뭘까?
2022년 09월 18일, 백준 에서 단계별로 문제를 풀고 있는데 같은 C++로 풀었음에도 불구하고 메모리 크기가 거의 반 정도 다른 것을 확인할 수 있다. 라는 궁금증으로부터 이 글이 작성되었다. 메모리 크기가 다른 두 코드를 살펴본 결과 다음과 같았다.

예전에 궁금했었던 부분이 더 체감되는 문제였다.

시간이 672ms 걸린 코드는 처음에 작성했던 코드이다. 그리고 그 코드는 다음과 같다.
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
long long N = 0;
long long numArr[1000001] = {};
cin >> N;
for (long long i = 0; i < N; i++)
{
cin >> numArr[i];
}
sort(numArr, numArr + N);
for (long long i = 0; i < N; i++)
{
cout << numArr[i]<<"\n";
}
}
속도가 반이나 줄어든 이유는 겨우 코드 2줄을 넣어준 게 전부였다.
ios_base::sync_with_stdio(false);
cin.tie(NULL);
캡처 때문에 속도가 더 느린 거 같지만 아무튼, 체감 속도는 느렸다… 🤓

Uploaded by N2T