간만에 팁을 올립니다. 올해의 첫번째 팁이네요.
이번엔 유행에 맞추어서 로또 숫자 뽑는 프로그램입니다. ^_^
STLport의 random_sample_n 알고리듬을 사용하면 한큐에 됩니다.
참고하세요.
//---------------------------------------------------------------------------
#include <iostream>
#include <cstdlib>
#ifndef __BORLANDC__
#include <ctime>
#endif
#pragma hdrstop
#include <iterator>
#include <algorithm>
#include <numeric>
//---------------------------------------------------------------------------
using namespace std;
#pragma argsused
int main(int argc, char* argv[])
{
const int N = 45, R = 6;
int Balls[N];
iota(&Balls[0], &Balls[N], 1); // 1, 2, 3, ... 45 를 배열에 넣습니다.
#ifdef __BORLANDC__
randomize();
#else
srand(time());
#endif
// random_sample과 달리 원본 순서를 유지합니다. 그러므로 오름차순으로 출력됩니다.
random_sample_n(&Balls[0], &Balls[N], ostream_iterator<int>(cout, " "), R);
cout << endl;
return 0;
}
//---------------------------------------------------------------------------
|