From 81386b6b6c7da24bcbaa097955737fc0d5a0c876 Mon Sep 17 00:00:00 2001 From: pk6gv5h38 <2932140327@qq.com> Date: Sun, 29 Oct 2023 11:09:08 +0800 Subject: [PATCH] ADD file via upload --- 作业2.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 作业2.c diff --git a/作业2.c b/作业2.c new file mode 100644 index 0000000..0f82a6c --- /dev/null +++ b/作业2.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include + +void generateMatrix(int matrix[9][9]) { + srand(time(0)); // ʹÓõ±Ç°Ê±¼ä×÷ΪËæ»úÊýÖÖ×Ó + + // ³õʼ»¯¾ØÕó + int i,j; + for (i = 0; i < 9; i++) { + for (j = 0; j < 9; j++) { + matrix[i][j] = 0; + } + } + + // Éú³ÉÿÐеÄÊý×Ö + int row; + for (row = 0; row < 9; row++) { + // Éú³É1-9µÄËæ»úÅÅÁÐ + int nums[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + for (i = 0; i < 9; i++) { + int j = rand() % (i + 1); + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } + + // ½«Ç°Èý¸öËæ»úÊý·ÅÈë¾ØÕó + for ( i = 0; i < 3; i++) { + matrix[row][i] = nums[i]; + } + } + + // ´òÂÒÿÐеÄ˳Ðò + for (row = 0; row < 9; row++) { + for ( i = 0; i < 9; i++) { + int j = rand() % 9; + int temp = matrix[row][i]; + matrix[row][i] = matrix[row][j]; + matrix[row][j] = temp; + } + } +} + +void printMatrix(int matrix[9][9]) {int i,j; + for (i = 0; i < 9; i++) { + for (j = 0; j < 9; j++) { + printf("%d ", matrix[i][j]); + } + printf("\n"); + } +} + +int main() { + int matrix[9][9]; + + generateMatrix(matrix); + printMatrix(matrix); + + return 0; +}