當前位置:商標查詢大全網 - 彩票開獎 - C++編程:寫壹個產生K組福彩M選N的機選彩票號碼程序(如K=5, M=35, N=7,表示機選5組35選7的號碼)。

C++編程:寫壹個產生K組福彩M選N的機選彩票號碼程序(如K=5, M=35, N=7,表示機選5組35選7的號碼)。

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <time.h>

void selectsort(int *x, int n) {

int i, j, min,t;

for (i = 0; i < n - 1; i++) {

min = i;

for (j = i + 1; j < n; j++) {

if (*(x + j) < *(x + min)) min = j;

}

if (min != i) {

t = *(x + i);

*(x + i) = *(x + min);

*(x + min) = t;

}

}

}

void select(int m,int n,int k) {

int num[36],tmp[36],t;

char filename[] = "C:\\2011\\隨機號碼.txt"; // 選擇妳需要的路徑

FILE *fp;

srand((unsigned)time(NULL));

fp = fopen(filename,"wt");

if(fp == NULL) {

printf("打開文件%s錯。\n",filename);

exit(1);

}

for(int i = 0;i < k;++i) {

for(int j = 1;j <= m;++j) tmp[j] = j;

for(j = 0;j < n;++j) {

t = (unsigned)rand()%m + 1;

if(tmp[t] != 0) {

num[j] = t;

tmp[t] = 0;

}

else --j;

}

selectsort(num,n);

for(j = 0;j < n;++j) {

fprintf(fp,"%02d ",num[j]);

printf("%02d ",num[j]);

}

fprintf(fp,"\n");

printf("\n");

}

fclose(fp);

}

int main() {

int m,n,k;

printf("被選號碼數 : ");

scanf("%d",&m);

printf("可選號碼數 : ");

scanf("%d",&n);

printf("所選組數 : ");

scanf("%d",&k);

select(m,n,k);

return 0;

}