Taene's

[백준] Class2-수 정렬하기 2 C++ 2751번 본문

백준/Class2

[백준] Class2-수 정렬하기 2 C++ 2751번

taene_ 2023. 8. 31. 09:37

문제

N개의 수가 주어졌을 때, 이를 오름차순으로 정렬하는 프로그램을 작성하시오.

입력

첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 수가 주어진다. 이 수는 절댓값이 1,000,000보다 작거나 같은 정수이다. 수는 중복되지 않는다.

출력

첫째 줄부터 N개의 줄에 오름차순으로 정렬한 결과를 한 줄에 하나씩 출력한다.

예제 입력 1 복사

5
5
4
3
2
1

예제 출력 1 복사

1
2
3
4
5

 

소스코드:

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n;	//수의 개수 1~1,000,000
	cin >> n;

	int k;
	vector<int> a;

	for (int i = 0; i < n; i++)
	{
		cin >> k;
		a.push_back(k);
	}

	sort(a.begin(), a.end());

	for (int i = 0; i < n; i++)
		cout << a[i] << "\n";
}

접근방법: #include <algorithm>의 sort함수는 오름차순으로 배열을 정렬한다.