pjc97uv2f 4 weeks ago
parent 8a7ab87af7
commit b75a9e75f4

55
2

@ -0,0 +1,55 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 9
void printMatrix(char matrix[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%c ", matrix[i][j]);
}
printf("\n");
}
}
int isUsed(int used[], int num) {
for (int i = 0; i < SIZE; i++) {
if (used[i] == num) return 1;
}
return 0;
}
void generateMatrix(char matrix[SIZE][SIZE]) {
int used[SIZE];
int count, num;
for (int block = 0; block < 3; block++) {
for (int i = 0; i < SIZE; i++) {
used[i] = 0;
}
for (int i = block * 3; i < (block + 1) * 3; i++) {
count = 0;
while (count < 3) {
num = rand() % 9 + 1;
if (!isUsed(used, num)) {
int position;
do {
position = rand() % SIZE;
} while (matrix[i][position] != '.');
matrix[i][position] = num + '0';
used[count] = num;
count++;
}
}
}
}
}
int main() {
srand(time(NULL));
char matrix[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
matrix[i][j] = '.';
}
}
generateMatrix(matrix);
printMatrix(matrix);
return 0;
}
Loading…
Cancel
Save