#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <algorithm> // std::sort, std::find, std::copy
#include <vector> // std::vector
void getRandomLottoWin(int *r) // 당첨번호+보너스번호(7수) 랜덤 생성
{
std::vector<int> vv; // vector vv 선언(중복검사, 정렬 등의 작업이 편하다)
while(true) {
int temp = rand()%45 + 1; // 1~45 사이의 랜덤값 추출
if(std::find(vv.begin(), vv.end(), temp)==vv.end()) { // 벡터 vv 에 temp 값이 없다면
vv.push_back(temp); // 벡터 vv 에 temp 값을 추가
if(vv.size()==7) break; // 7수 모두를 얻었다면 while 루프를 빠져 나감
}
}
std::sort(vv.begin(), vv.begin()+6); // 정렬(보너스번호 빼고)
std::copy(vv.begin(), vv.end(), r); // 복사
}
void getRandomLotto(int *r) // 로또번호(6수) 랜덤 생성
{
std::vector<int> vv; // vector vv 선언(중복검사, 정렬 등의 작업이 편하다)
while(true) {
int temp = rand()%45 + 1; // 1~45 사이의 랜덤값 추출
if(std::find(vv.begin(), vv.end(), temp)==vv.end()) { // 벡터 vv 에 temp 값이 없다면
vv.push_back(temp); // 벡터 vv 에 temp 값을 추가
if(vv.size()==6) break; // 6수 모두를 얻었다면 while 루프를 빠져 나감
}
}
std::sort(vv.begin(), vv.end()); // 정렬
std::copy(vv.begin(), vv.end(), r); // 복사
}
int checkWin(int *r1, int *r2)
{
for(int i=0; i<7; i++) {
if(r1[i]<1 || 45<r1[i]) return 0; // 로또번호+보너스볼의 무결성 체크 #1
if(i==6) break;
for(int j=i+1; j<7; j++) {
if(r1[i]==r1[j]) return 0; // 로또번호+보너스볼의 무결성 체크 #2
}
}
for(int i=0; i<6; i++) {
if(r2[i]<1 || 45<r2[i]) return 0; // 로또번호의 무결성 체크 #1
if(i==5) break;
for(int j=i+1; j<6; j++) {
if(r2[i]==r2[j]) return 0; // 로또번호의 무결성 체크 #2
}
}
// 등수 확인
int smallCount=0, bonusCount=0;
for(int i=0;i<7;i++) {
for(int j=0;j<6;j++) {
if(r1[i]==r2[j]) {
if(i==6) bonusCount++;
else smallCount++;
}
}
}
int winLevel=0;
if(smallCount<3) {
winLevel=0;
} else if(smallCount==3) {
winLevel = 5;
} else if(smallCount==4) {
winLevel = 4;
} else if(smallCount==5 && bonusCount==1) {
winLevel = 2;
} else if(smallCount==5 && bonusCount!=1) {
winLevel = 3;
} else if(smallCount==6) {
winLevel = 1;
}
return winLevel;
}
int main(int argc, char *argv[])
{
srand((unsigned)time(NULL));
int r1[7]; // 랜덤으로 생성된 로또번호+보너스번호 를 저장할 배열
int r2[6]; // 랜덤으로 생성된 로또번호를 저장할 배열
for(int i=0; i<100; i++) {
getRandomLottoWin(r1);
getRandomLotto(r2);
int winLevel = checkWin(r1, r2);
printf("%02d,%02d,%02d,%02d,%02d,%02d / %02d", r1[0],r1[1],r1[2],r1[3],r1[4],r1[5],r1[6]);
printf(" ::: %02d,%02d,%02d,%02d,%02d,%02d", r2[0],r2[1],r2[2],r2[3],r2[4],r2[5]);
if(1<=winLevel && winLevel<=5)
printf(" === %d", winLevel);
printf("\n");
}
printf("\n");
getchar();
return 0;
}
이를 비교하여 등수를 계산하여 출력하는 소스코드입니다.