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.

72 lines
1.3 KiB

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int board[9][9] = {0}, f[9] = {0};
void f1()
{
printf("|");
for (int i = 0; i < 23; i++)
{
printf("-");
}
printf("|\n");
}
void operate()
{
for (int i = 0; i < 9; i++)
{
if (i % 3 == 0)
{
f1();
}
printf("| ");
for (int j = 0; j < 9; j++)
{
if (j % 3 == 0 && j)
{
printf("| ");
}
printf("%d ", board[i][j]);
}
printf("|\n");
}
f1();
}
void Random(int n)
{
int k = 9;
while (k)
{
int i = rand() % 3, j =rand() % 9;
i += (n - 1) * 3;
if (f[i] == 0)
{
if (board[i][j] == 0)
{
board[i][j] = k;
k--;
}
}
int cnt = 0;
for (int m = 0; m < 9; m++)
{
if (board[i][m])
{
cnt++;
}
}
if (cnt == 3)
{
f[i] = 1;
}
}
}
int main()
{
printf("随机生成一个9×9的不完整的矩阵\n");
srand(time(NULL));
Random(1);
Random(2);
Random(3);
operate();
}