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.
55 lines
1000 B
55 lines
1000 B
#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;
|
|
} |