From b75a9e75f42e773f33ccf00759490cb4ef1504aa Mon Sep 17 00:00:00 2001 From: pjc97uv2f <1291955673@qq.com> Date: Sat, 2 Nov 2024 10:20:01 +0800 Subject: [PATCH] Add 2 --- 2 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 2 diff --git a/2 b/2 new file mode 100644 index 0000000..a185cf5 --- /dev/null +++ b/2 @@ -0,0 +1,55 @@ +#include +#include +#include +#define SIZE 9 +void printMatrix(char matrix[SIZE][SIZE]) { +for (int i = 0; i < SIZE; i++) { +for (int j = 0; j < SIZE; j++) { +printf("%c ", matrix[i][j]); +} +printf("\n"); +} +} +int isUsed(int used[], int num) { +for (int i = 0; i < SIZE; i++) { +if (used[i] == num) return 1; +} +return 0; +} +void generateMatrix(char matrix[SIZE][SIZE]) { +int used[SIZE]; +int count, num; +for (int block = 0; block < 3; block++) { +for (int i = 0; i < SIZE; i++) { +used[i] = 0; +} +for (int i = block * 3; i < (block + 1) * 3; i++) { +count = 0; +while (count < 3) { +num = rand() % 9 + 1; +if (!isUsed(used, num)) { +int position; +do { +position = rand() % SIZE; +} while (matrix[i][position] != '.'); +matrix[i][position] = num + '0'; +used[count] = num; +count++; +} +} +} +} +} + +int main() { +srand(time(NULL)); +char matrix[SIZE][SIZE]; +for (int i = 0; i < SIZE; i++) { +for (int j = 0; j < SIZE; j++) { +matrix[i][j] = '.'; +} +} +generateMatrix(matrix); +printMatrix(matrix); +return 0; +} \ No newline at end of file