/***************************************************************************
 *            Lotto.c
 *
 *  Sun Nov  4 01:12:21 2007
 *  Copyright  2007  pchero21
 *  pchero21@gmail.com
 ****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place – Suite 330, Boston, MA 02111-1307, USA.
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define LOTTO_COUNT 6
#define LOTTO_MAX 45
#define NUM_LOTATE 100000

struct Lotto_array {
    int lotto_number;
    int number_count;
};

void quickSort(int low, int high, struct Lotto_array *a);
void swap(struct Lotto_array *a, struct Lotto_array *b);
void partition(int low, int high, int *pivotpoint, struct Lotto_array *S)
;

int main()
{
    int i, j, k, tmp[6];
    struct Lotto_array lotto_array[LOTTO_MAX];
    struct timespec timecheck;
        
    for(i = 0; i < 45; i++) {
        lotto_array[i].lotto_number = i + 1;
        lotto_array[i].number_count = 0;
    }
    
    for(i = 0; i < NUM_LOTATE; i++) {
        clock_gettime(CLOCK_REALTIME, &timecheck);
        srand((unsigned)timecheck.tv_nsec * 1000000000);
        
        for(j = 0; j < LOTTO_COUNT; j++) {
            tmp[j] = (rand() % 45);
        
            for(k = 0; k < j; k++) {
                if(tmp[k] == tmp[j]) {
                    j–;
                    break;
                }
            }
        }
        
        for(j = 0; j < LOTTO_COUNT; j++) {
            lotto_array[tmp[j]].number_count++;
    //        printf(“%d “, tmp[j]);
        }
        putchar(‘n’);
    }

    quickSort(0, 44,lotto_array);
    

    printf(“The Lotto number counting!!n”);
    
    for(i = 0; i < LOTTO_MAX; i++)
        printf(“%d th : %d : %dn”, i + 1, lotto_array[i].lotto_number, lotto_array[i].number_count);
    
    return 0;
}

void quickSort(int low, int high, struct Lotto_array *a)
{
    int pivotpoint;
 
    if(high > low) {
        partition(low, high, &pivotpoint, a);
        quickSort(low, pivotpoint – 1, a);
        quickSort(pivotpoint + 1, high, a);
    }
}

void partition(int low, int high, int *pivotpoint, struct Lotto_array *S)
{
    int i, j;
    int pivotitem;

    pivotitem = S[low].number_count;
    j = low;
    for(i = low + 1; i <= high; i++) {
        if(S[i].number_count > pivotitem) {
            j++;
            swap(&S[i], &S[j]);
        }
    }
    *pivotpoint = j;
    swap(&S[low], &S[*pivotpoint]);
}
 
void swap(struct Lotto_array *a, struct Lotto_array *b)
{
    struct Lotto_array temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

로또를 10만 게임을 해서 가장 많이 Hit 한 로또 번호 순서대로 출력하는 프로그램.
퍼가실때 출처는 밝혀주세요.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.