當前位置:商標查詢大全網 - 彩票開獎 - C語言彩票20選5問題求解!

C語言彩票20選5問題求解!

TC 2.0 通過測試,自己測試下看結果是否正確。

#include <stdlib.h>

#include <mem.h>

#include <stdio.h>

#define FILE1 "data.txt"

#define FILE2 "temp.txt"

#define FILE3 "result.txt"

#define NUM1 8

#define NUM2 5

FILE *fp = NULL;

int * sum = NULL;

int sum1, sum2, index = 0;

int number[NUM1];

int result[NUM2];

void init()

{

int i, s, t;

printf("系統正在初始化。。。\n");

printf("請輸入和值範圍:");

scanf("%d %d", &sum1, &sum2);

getchar();

s = 1;

t = NUM2;

for (i = 0; i < 5 ; ++i)

s *= (t-i);

sum = (int *)malloc(sizeof(int) * s);

memset(sum, 0, sizeof(int) * s);

for (i = 0; i < NUM1; ++i)

number[i] = i+1;

fp = fopen(FILE1, "w");

if (fp == NULL)

{

printf("無法打開文件!\n");

exit(-1);

}

}

void asort(int *a, int s)

{

int i, j, k, t;

for (i = 0; i < s-1; ++i)

{

k = i;

for (j = i + 1; j < s; ++j)

{

if (a[k] > a[j])

{

k = j;

}

}

if (k != i)

{

t = a[i];

a[i] = a[k];

a[k] = t;

}

}

}

void traverse(int cnt)

{

int i, j, f;

if (cnt == NUM2)

{

int s = 0;

int a[NUM2];

for (i = 0; i < NUM2; ++i)

s += result[i];

if (s < sum1 || s > sum2)

return;

for (i = 0; i < index; ++i)

{

if (s == sum[i])

return;

}

sum[index++] = s;

memcpy(a, result, sizeof(int) * NUM2);

asort(a, NUM2);

for (i = 0; i < NUM2-1; ++i)

{

fprintf(fp, "%d ", a[i]);

printf("%d ", a[i]);

}

fprintf(fp, "%d\n", a[i]);

printf("%d\n", a[i]);

return;

}

for (i = 0; i < NUM1; ++i)

{

f = 0;

for (j = 0; j < cnt; ++j)

{

if (result[j] == number[i])

{

f = 1;

break;

}

}

if (f == 1)

continue;

result[cnt] = number[i];

traverse(cnt+1);

}

}

void clear()

{

free(sum);

printf("操作成功完成!結果已保存到result.txt文件。\n");

printf("系統正在清理中。。。\n");

fclose(fp);

unlink(FILE1);

unlink(FILE2);

}

void main()

{

FILE *fp1;

int i, f, t, filter[NUM2];

char choice;

init();

traverse(0);

fflush(fp);

fclose(fp);

fp = fopen(FILE1, "r");

fp1 = fopen(FILE2, "w");

if (fp == NULL || fp1 == NULL)

{

printf("無法打開文件!\n");

exit(-1);

}

do

{

printf("請輸入壹個過濾數字:");

scanf("%d", &t);

getchar();

/*

if (t < number[0] || t > number[NUM1-1])

break;

*/

while (1)

{

f = 0;

memset(result, 0, sizeof(int) * NUM2);

for (i = 0; i < NUM2; ++i)

fscanf(fp, "%d", result + i);

if (result[0] == 0)

break;

for (i = 0; i < NUM2; ++i)

{

if (t == result[i])

{

f = 1;

break;

}

}

if (f == 1)

continue;

for (i = 0; i < NUM2-1; ++i)

{

printf("%d ", result[i]);

fprintf(fp1, "%d ", result[i]);

}

printf("%d\n", result[i]);

fprintf(fp1, "%d\n", result[i]);

}

fclose(fp);

fclose(fp1);

unlink(FILE1);

rename(FILE2, FILE1);

fp = fopen(FILE1, "r");

fp1 = fopen(FILE2, "w");

printf("是否繼續執行過濾(y/n)?");

choice = getchar();

getchar();

}

while (choice == 'y' || choice == 'Y');

fclose(fp);

fclose(fp1);

fp = fopen(FILE1, "r");

fp1 = fopen(FILE3, "w");

if (fp == NULL || fp1 == NULL)

{

printf("無法打開文件!\n");

exit(-1);

}

printf("請輸入需要過濾的壹組數:");

for (i = 0; i < NUM2; ++i)

scanf("%d", filter+i);

for (i = 0; i < NUM2; ++i)

printf("%d ", filter[i]);

getchar();

f = 0;

while (1)

{

memset(result, 0, sizeof(int) * NUM2);

for (i = 0; i < NUM2; ++i)

fscanf(fp, "%d", result + i);

if (result[0] == 0)

break;

i = 0;

while ((i < NUM2) && (filter[i] == result[i++]));

if (i == NUM2)

continue;

for (i = 0; i < NUM2-1; ++i)

fprintf(fp1, "%d ", result[i]);

fprintf(fp1, "%d\n", result[i]);

}

fclose(fp1);

clear();

system("PAUSE");

}