diff --git a/t2.c b/t2.c new file mode 100644 index 0000000..e48d65f --- /dev/null +++ b/t2.c @@ -0,0 +1,72 @@ +#include +#include +#include +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(); +} \ No newline at end of file