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; +}