Compare commits

..

1 Commits

Author SHA1 Message Date
p9gzklfo6 bedac8f01c ADD file via upload
2 weeks ago

54
2.cpp

@ -0,0 +1,54 @@
#include <iostream>
#include <stdlib.h>
#include <time.h>
int main()
{
int board[9][9] = {0};
srand(time(NULL));
void generate(int board[9][9]);
void format(int board[9][9]);
generate(board);
format(board);
}
void random(int arr[9]) {
for (int i = 0; i < 9; i++) {
arr[i] = rand() % 9 + 1;
Re:
for (int j = 0; j < i; j++) {
while (arr[i] == arr[j]) {
arr[i] = rand() % 9 + 1;
goto Re;
}
}
}
}
void format(int board[9][9]) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
printf("%d ", board[i][j]);
if ((j + 1) % 3 == 0 && j != 8) {
printf("| ");
}
}
if ((i + 1) % 3 == 0 && i != 8) {
printf("\n---------------------\n");
}
else { printf("\n"); }
}
}
void generate(int board[9][9]) {
int rowplace[9] = { 0 }, rownum[9] = { 0 };
for (int h = 0; h < 9; h += 3) {
random(rownum);
for (int i = h, k = -1; i < h + 3; i++) {
random(rowplace);
for (int j = 0; j < 9; j++) {
if (rowplace[j] < 4) {
k++;
board[i][j] = rownum[k];
}
else board[i][j] = 0;
}
}
}
}
Loading…
Cancel
Save