diff --git a/2.cpp b/2.cpp new file mode 100644 index 0000000..643fdc0 --- /dev/null +++ b/2.cpp @@ -0,0 +1,55 @@ +#include +#include +#include + +void matrixOutput(int matrixArr[9][9]) +{ + for (int i = 0; i < 9; i++) + { + for (int j = 0; j < 9; j++) + { + printf("%d", matrixArr[i][j]); + if (j != 8) + printf(" "); + } + if (i != 8) + printf("\n"); + } +} + +void matrixRandomInit(int matrixArr[9][9]){ + int map[9] = {1,2,3,4,5,6,7,8,9}; + int mp; + srand(time(NULL)); + for(int i=0;i<9;i++){ + for(int j=0;j<9;j++){ + matrixArr[i][j] = 0; + } + } + for(int i=0;i<9;i++){ + if(i%3==0){ + mp=0; + for(int j=0;j<8;j++){ + int n = i+rand()%(8-i); + int tmp = map[i]; + map[i] = map[n]; + map[n]=tmp; + } + } + int k=0; + while(k<3){ + int tmpP = rand()%9; + if(matrixArr[i][tmpP]==0){ + matrixArr[i][tmpP] = map[mp++]; + k++; + } + } + } +} + +int main(){ + int board[9][9]; + matrixRandomInit(board); + matrixOutput(board); + return 0; +}