Created
October 26, 2020 03:35
-
-
Save alexparkjw/c7f55de82d989a7ae7963315b1a59d64 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
nCr, nPr, 10C4=210, 10P4=5040, nCr=nPr/r! | |
*/ | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
#include<time.h> | |
void swap(void *vp1, void *vp2, int size) { | |
char buffer[size]; | |
memcpy(buffer, vp1, size); | |
memcpy(vp1, vp2, size); | |
memcpy(vp2, buffer, size); | |
} | |
void combi(FILE *fp, int *arr,int *data,int start,int end,int index,int r) { | |
if (index == r) { | |
for (int j=0; j<r; j++) fprintf(fp, "%d", data[j]); | |
fprintf(fp, "\n"); | |
return; | |
} | |
for (int i=start; i<=end && end-i+1 >= r-index; i++) { | |
data[index] = arr[i]; | |
combi(fp, arr, data, i+1, end, index+1, r); | |
} | |
} | |
void permute(FILE *fp, char *arr, int left, int right) { | |
if (left == right) | |
fprintf(fp, "%s\n", arr); | |
else { | |
for (int i = left; i <= right; i++) { | |
swap((arr+left), (arr+i), sizeof(char)); | |
permute(fp, arr, left+1, right); | |
swap((arr+left), (arr+i), sizeof(char)); | |
} | |
} | |
} | |
int strike(char *arr, char *dd) { | |
int ss=0; | |
for(int i=0; i<4; i++) | |
if(arr[i]==dd[i]) ss++; | |
return ss; | |
} | |
int ball(char *arr, char *dd) { | |
int bb=0; | |
for(int i=0; i<4; i++) | |
for(int j=0; j<4; j++) | |
if(arr[i]==dd[j] && i!=j) bb++; | |
return bb; | |
} | |
int main(void) { | |
FILE * fp=fopen("com.txt", "w+"); | |
FILE * fw=fopen("per.txt", "w+"); | |
if(fp==NULL) { printf("can not open file com.txt \n"); exit(-1); } | |
if(fw==NULL) { printf("can not open file per.txt \n"); exit(-1); } | |
// aa-0,1,2,3,4,5,6,7,8,9 dd-4 unknowns | |
int *aa=(int*)malloc(sizeof(int)*10); | |
int *dd=(int*)malloc(sizeof(int)*4); | |
char str[10]; | |
for(int i=0; i<10; i++) aa[i]=i; | |
combi(fp, aa, dd, 0, 10-1, 0, 4); | |
rewind(fp); | |
for(int i=0; i<210; i++) { | |
fscanf(fp, "%s", str); | |
permute(fw, str, 0, 4-1); | |
} | |
rewind(fw); | |
// ss-strike count , bb-ball count, kk-guess count | |
int ss, bb, kk=0; | |
char **arr=(char**)malloc(sizeof(char*)*5040); | |
char **brr=(char**)malloc(sizeof(char*)*5040); | |
for(int i=0; i<5040; i++) { | |
arr[i]=(char*)malloc(sizeof(char)*4+1); | |
brr[i]=(char*)malloc(sizeof(char)*4+1); | |
fscanf(fw, "%s", arr[i]); | |
strcpy(brr[i], arr[i]); | |
} | |
srand(time(NULL)); | |
int ratio=5040, temp; | |
char ran[5]; | |
strcpy(ran, arr[rand()% 5040]); | |
printf("starting ratio (5040) : %.2f%% \n", 100/(float)ratio); | |
for(int j=0; j<9; j++) { | |
re: // repeat gen | |
temp=rand()%(5040-kk); | |
if ( strcmp(arr[temp], "----")==0 ) goto re; | |
swap(arr[temp], arr[5040-(kk+1)], 4); | |
kk++; | |
printf("\n<< %d inning >>\n", j+1); | |
printf("random number : %s\n", ran); // check | |
printf("computer off. : %s\n", arr[5040-kk]); | |
// manual input | |
/* printf("%d. # strikes, # balls : ", j+1); */ | |
/* scanf("%d %d", &ss, &bb); */ | |
// auto input | |
ss=strike(ran, arr[5040-kk]); | |
bb=ball(ran, arr[5040-kk]); | |
if(ss==4) printf(" homerun!! \n"); | |
else if(ss==0 && bb==0) printf(" out!! \n"); | |
else printf(" %d strikes, %d balls .\n", ss, bb); | |
if(ss == 4) break; | |
ratio=0; | |
for(int i=0; i<5041-kk; i++) { | |
if(strike(arr[i], arr[5040-kk])==ss | |
&& ball(arr[i], arr[5040-kk])==bb | |
&& strcmp(arr[i], "----") ) { | |
//printf("%s\n", arr[i]); | |
ratio++; | |
} else strcpy(arr[i], "----"); } | |
printf(" ratio (%d) : %.2f%% \n",ratio, 100/(float)ratio); } | |
// free & exit | |
for(int i=0; i<5040; i++) free(arr[i]); free(arr); | |
free(aa); free(dd); fclose(fp); fclose(fw); | |
return 0; } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment