源代码如下:
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> int ShuangSeQiu[16][7]; int flags[33]; int IsValid(int num) { return flags[num-1]; } void Increment(int num) { flags[num-1]--; //printf("%d ", num); } int suijishu(void) { int i; while(1) { i = rand() % 100 ; if((i >= 1) && (i <= 33)) { if(flags[i-1]) { //flags[i-1]--; //printf("%d ", i); return i; } } } } void numgen(int index) { int t1, t2, t3, t4, t5, t6; t1 = suijishu(); Increment(t1); do { t2 = suijishu(); }while(t2 == t1); Increment(t2); do { t3 = suijishu(); }while((t3 == t1) || (t3 == t2)); Increment(t3); do { t4 = suijishu(); }while((t4 == t1) || (t4 == t2) || (t4 == t3)); Increment(t4); do { t5 = suijishu(); }while((t5 == t1) || (t5 == t2) || (t5 == t3) || (t5 == t4)); Increment(t5); do { t6 = suijishu(); }while((t6 == t1) || (t6 == t2) || (t6 == t3) || (t6 == t4) || (t6 == t5)); Increment(t6); printf("\n"); ShuangSeQiu[index-1][0] = t1; ShuangSeQiu[index-1][1] = t2; ShuangSeQiu[index-1][2] = t3; ShuangSeQiu[index-1][3] = t4; ShuangSeQiu[index-1][4] = t5; ShuangSeQiu[index-1][5] = t6; } void InsertionSort(int A[], int N) { int j,P; int Tmp; for(P = 1; P < N; P++) { Tmp = A[P]; for(j = P; (j > 0) && (A[j-1] > Tmp); j--) A[j] = A[j-1]; A[j] = Tmp; } } void sort(void) { for(int i = 1; i <= 16; i++) { InsertionSort(ShuangSeQiu[i-1], 6); } } void output(void) { FILE* fp; char str[32]; fp = fopen("out.txt", "w"); for(int i = 1; i <= 16; i++) { sprintf(str, "%02d %02d %02d %02d %02d %02d:%02d\n", ShuangSeQiu[i-1][0], ShuangSeQiu[i-1][1], ShuangSeQiu[i-1][2], ShuangSeQiu[i-1][3], ShuangSeQiu[i-1][4], ShuangSeQiu[i-1][5], ShuangSeQiu[i-1][6]); fwrite(str, strlen(str), 1, fp); } fclose(fp); } int main(void) { printf("Hello World!\n\r"); for(int j = 1; j <= 33; j++) { flags[j-1] = 3; } srand((unsigned)time(NULL)); for(int i = 1; i <= 16; i++) { ShuangSeQiu[i-1][6] = i; numgen(i); } sort(); output(); }