이제까지 원소의 순서를 변경하는 몇가지 알고리듬을 소개했습니다.
잠깐 정리하고 넘어가도록 하죠. 번호는 게시판의 글 번호입니다.
6) random_shuffle: 원소를 임의로 뒤섞기
41) random_sample, ramdom_sample_n: 임의 추출
14) sort: 모든 원소 정렬
49) partial_sort: 일부만 정렬
14) partition, stable_partition: 조건을 만족하는 원소를 앞쪽으로 이동
23) nth_element: 정렬했을 경우에 n번째 원소가 위치가 놓이는 위치로 놓고 시퀀스를 파티션
21, 49) pop_heap, push_heap, make_heap, sort_heap: 힙 연산
45) next_permutation, prev_permutation : 순열 계산
이 외에 아직 다루지 않은 것으로 rotate, rotate_copy, reverse, reverse_copy가 있습니다.
오늘은 이중에서 rotate_copy의 예제를 들어보죠. 이것만 보면 나머지의 사용법도 금방 아실 수 있겠죠?
------------------------------------------------------------------------------------------
: 다음과 같이 N 과 M을 입력받아
:
: N = 1
: M = 5
:
: 다음과 같이 출력하는 프로그램을 작성하라.
:
: 1 2 3 4 5
: 5 1 2 3 4
: 4 5 1 2 3
: 3 4 5 1 2
: 2 3 4 5 1
//---------------------------------------------------------------------------
#include <iostream>
#pragma hdrstop
#include <vector>
#include <algorithm>
#include <numeric>
#include <iterator>
//---------------------------------------------------------------------------
using namespace std;
int main()
{
int N, M;
cout << "N = "; cin >> N;
cout << "M = "; cin >> M;
vector<int> series(M);
iota(series.begin(), series.end(), N);
ostream_iterator<int> out(cout, " ");
copy(series.begin(), series.end(), out);
cout.put('\n');
for (int i = 1; i < M; ++i) {
rotate_copy(series.begin(), series.end() - i, series.end(), out);
cout.put('\n');
}
return 0;
}
|