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.
63 lines
1.4 KiB
63 lines
1.4 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <time.h>
|
|
|
|
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;
|
|
}
|