ADD file via upload

ver2.0
psoyatper 1 year ago
parent 8275d726bf
commit 4fad643db7

@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void create(int board[9][9]);
void output(int board[9][9]);
int main(){
int board[9][9] = {0};
create(board);
output(board);
return 0;
}
void shuffleArray(int array[], int size) {
for (int i = size - 1; i > 0; i--) {
int j = rand() % (i + 1);
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
void create(int board[9][9]){
srand(time(NULL));
int index = 0;
for (int r = 0; r < 9; r+=3){
int nums[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
shuffleArray(nums, 9);
for (int i = r; i < r+3; i++) {
for (int j = 0; j < 9; j += 3) {
board[i][j] = nums[index++];
if (index >= 9) {
index = 0;
}
}
}
}
for(int j = 0;j < 9;j++){
shuffleArray(board[j], 9);
}
}
void output(int board[9][9]){
int i;
for(i = 0;i < 9 ;i++){
if(i %3 == 0){
printf("|-----------------------|\n");
}
printf("| %d %d %d | %d %d %d | %d %d %d |\n",
board[i][0],board[i][1],board[i][2],board[i][3],board[i][4],board[i][5],board[i][6],board[i][7],board[i][8]);
}
printf("|-----------------------|\n");
}
Loading…
Cancel
Save