这是一个九宫填数的程序,用的是穷举的方法~
偶是菜鸟~~不只这中有什么逻辑错误
请各位帮帮~~小弟先说谢谢了啊
int A[9],M[3][3];
void print(int mm){ int j; FILE* ff; ff=fopen("result.txt","w"); if (mm==1){ for (j=1;j<=9;j++){ if (!(j%3)) fputc('\n',ff); fprintf(ff,"%d ",M[(j-1)/3][(j-1)%3]); } fclose(ff); exit(0); }else{ fputs("Not Fount!\n",ff); fclose(ff); } } int check(){ int i,j; int temp1=0, temp2=0, temp3=0, temp4=0, temp5=0, temp6=0; for (i=0;i<3;i++){ for (j=0;j<3;j++){ temp1+=M[i][j]; temp2+=M[j][i]; } if (temp1!=temp2) return 0; if ((temp3!=0)&&(temp4!=0)&&(temp3!=temp1)&&(temp4!=temp2)) return 0; temp3=temp1; temp4=temp2; } for (i=0;i<3;i++){ temp5+=M[i][i]; temp6+=M[2-i][2-i]; } if((temp5!=temp6)&&(temp4!=temp5)) return 0; return 1; }
int main(){ int i,j,temp=0;
for (i=1;i<=9;i++) A[i-1]=i; printf("Please wait a minuts......"); i=0; while(1){ i=i%8; temp=A[i]; A[i]=A[i+1]; A[i+1]=temp; i++;
for (j=0;j<9;j++) M[j/3][j%3]=A[j]; if (check()){ print(1); } }print(0); return 1; }