You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.2 KiB

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 9
void print(int matrix[9][9]);
int main()
{
srand(time(NULL));
int matrix[9][9]={0};
int i=1,j=1;
int col=0;
int val=0;
int k=0,l=0; //<2F><>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
int isrepeat=0;
for(i=0;i<9;i++) //ÿһ<C3BF><D2BB>
{
for(j=0;j<3;j++) //<2F><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
{
label_col:
col=rand()%9;
if(matrix[i][col]!=0)
{
goto label_col; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5>ж<EFBFBD>Ӧ<EFBFBD><D3A6>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
else
{
if(i>=0&&i<3)
{
label_val1:
val=rand()%9+1; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
isrepeat=0; //<2F>˱<EFBFBD><CBB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
for(k=0;k<3;k++) //<2F><>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ɵ<EFBFBD>ֵ<EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
{
for(l=0;l<9;l++)
{
if(val==matrix[k][l])
{
isrepeat=1;
goto label_val1;
}
}
}
}
else if(i>=3&&i<6)
{
label_val2:
val=rand()%9+1; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
isrepeat=0;
for(k=3;k<6;k++)
{
for(l=0;l<9;l++)
{
if(val==matrix[k][l])
{
isrepeat=1;
goto label_val2;
}
}
}
}
else if(i>=6&&i<9)
{
label_val3:
val=rand()%9+1; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
isrepeat=0;
for(k=6;k<9;k++)
{
for(l=0;l<9;l++)
{
if(val==matrix[k][l])
{
isrepeat=1;
goto label_val3;
}
}
}
}
if(isrepeat==0)
{
matrix[i][col]=val;
}
}
}
}
print(matrix);
// for(i=0;i<9;i++)
// {
// for(j=0;j<9;j++)
// {
// printf("%d ",matrix[i][j]);
// }
// printf("\n");
// }
return 0;
}
void print(int matrix[9][9])
{
int i=0,j=0;
printf("|-----------------------------|\n");
for ( i = 0; i < N; i++) {
printf("|");
for (j = 0; j < N; j++) {
printf("%2d ", matrix[i][j]);
if ((j + 1) % 3 == 0 ) printf("|");
}
printf("\n");
if ((i + 1) % 3 == 0 && i < 8) {
printf("|-----------------------------|\n");
}
}
printf("|-----------------------------|\n");
}