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.

101 lines
2.3 KiB

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void assignNumbers(int matrix[3][9])
{
srand(time(NULL));
int num[10]={0,1,2,3,4,5,6,7,8,9};
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
int randomNumber = rand() % 10;
int random=rand()%10;
while (matrix[i][randomNumber] != 0)
{
randomNumber = rand() % 10;
}
while(num[random]==10)
{
random=rand()%10;
}
matrix[i][randomNumber] =num[random] ;
num[random]=10;
}
}
}
void shuffleMatrix(int matrix[3][9])//洗牌算法
{
for (int i = 2; i >= 0; i--)
{
for (int j = 8; j > 0; j--)
{
int row1 = i;
int col1 = j;
int row2 = rand() % (i + 1);
int col2 = rand() % (j + 1);
int temp = matrix[row1][col1];
matrix[row1][col1] = matrix[row2][col2];
matrix[row2][col2] = temp;
}
}
}
void mergeMatrices(int matrix1[3][9], int matrix2[3][9], int matrix3[3][9], int mergedMatrix[9][9]) //合并矩阵
{
// 复制第一个矩阵的内容到前三行
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 9; j++) {
mergedMatrix[i][j] = matrix1[i][j];
}
}
// 复制第二个矩阵的内容到中间三行
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 9; j++) {
mergedMatrix[i + 3][j] = matrix2[i][j];
}
}
// 复制第三个矩阵的内容到最后三行
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 9; j++) {
mergedMatrix[i + 6][j] = matrix3[i][j];
}
}
}
void print(int board[9][9])
{
for (int i = 0; i < 9; i++) //行
{
if (i % 3 == 0 )
{
printf("---------------------\n");
}
for (int j = 0; j < 9; j++) //列
{
if (j % 3 == 0 && j != 0)
{
printf("| ");
}
printf("%d ", board[i][j]);
}
printf("\n");
}
}
int main()
{
int merge[9][9]={0};
int mat1[3][9]={0};
int mat2[3][9]={0};
int mat3[3][9]={0};
assignNumbers(mat1);
assignNumbers(mat2);
assignNumbers(mat3);
mergeMatrices(mat1,mat2,mat3,merge);
print(merge);
return 0;
}