본문 바로가기

C++ in Windows/ETC

[콘솔] 로또 번호 추출. 응용1.


저번주 로또 추출기 결과를 보고 응용한 프로그램을 만들어보았다.


저번주에 기존에 나온 번호로 조합했을 경우 당첨되는 경우가 있었다.

3게임에서 6개 번호가 다 나왔지만, 게임당 2개씩 나와 5등도 안되는 결과였다.


그래서 기존 3게임의 번호로 테이블을 만들어.

그 테이블 안에서 6개 번호를 추첨해 2게임의 번호를 만드는 추출기를 만들었다.


5게임중 앞에 3게임은 테이블에 따로 저장해주고,

2게임은 테이블에서 추출해서 번호를 만든다.


10게임일경우에는 앞에 3게임 저장, 2게임 추출, 3게임 저장, 2게임 추출의 형식을 따른다.


이번주를 기대해봐야겠다. (어차피 안될놈은 안된다.)



#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <vector>
#include <algorithm>

using namespace std;

#define vTable	vector<int>
#define vLotto	vector<int>

vLotto GetLotto(vTable pTable)
{
	vLotto rLotto;
	
	for(int n = 0; n < 6; n++)
	{
		int index;
		
retry:
		index = rand() % pTable.size();
		vLotto ::iterator ck = find(rLotto.begin(), rLotto.end(), pTable[index]);

		if ( ck == rLotto.end() )
			rLotto.push_back(pTable[index]);
		else
			goto retry;
	}

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

	return rLotto;
}

void SaveNumber(vTable * saveTable, const int & number)
{
	vTable::iterator ck = find(saveTable->begin(), saveTable->end(), number);

	if ( ck == saveTable->end() ) saveTable->push_back(number);
}

void main (void)
{
	int num = 0;
	srand(time(NULL));

	printf("원하는 로또 게임수를 입력하세요.\n5개 게임중 2개의 \
                 게임은 그전 3개의 게임에서 나온 번호를 기반으로 출력됩니다.\n> ");

	scanf("%d", &num);

	vTable Table;
	vTable saveTable;

	for(int i = 1; i < 46; i++)
		Table.push_back(i);

	for(int i = 0; i < num; i++)
	{
		bool saveLotto = (i % 5 == 3) || ( i % 5 == 4) ? true : false;

		vLotto lotto;
		if ( saveLotto )
			lotto = GetLotto(saveTable);
		else
			lotto = GetLotto(Table);

		for(int n = 0; n < 6; n++)
		{
			if ( saveLotto == false)
				SaveNumber(&saveTable, lotto[n]);

			printf("%d ", lotto[n]);
		}

		printf("\n");

		if ( i % 5 == 4 ) saveTable.clear();
		lotto.clear();
	}
	
	fflush(stdin);
	system("pause");
	
	return;
}



아래 exe 파일은 Visual Studio 2005에서 제작한 실행파일이다.

Lotto 응용 1.exe


혹여나 실행이 안될경우. 아래 글에서 해당 패키지를 받아서 설치하면 된다.

Visual C++ 재배포 패키지



'C++ in Windows > ETC' 카테고리의 다른 글

Visual C++ 재배포 패키지  (0) 2014.06.02
[콘솔] 로또 번호 추출  (0) 2014.05.23