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.
79 lines
1.4 KiB
79 lines
1.4 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
|
|
#define SIZE 9
|
|
|
|
void fillArr(int arr[SIZE][SIZE])
|
|
{
|
|
for (int block = 0; block < SIZE; block += 3)
|
|
{
|
|
int seen[10] = {0};
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
int count = 0;
|
|
while (count < 3)
|
|
{
|
|
int pos = rand() % SIZE;
|
|
if (arr[block + i][pos] == 0)
|
|
{
|
|
int num = rand() % 9 + 1;
|
|
if (!seen[num])
|
|
{
|
|
arr[block + i][pos] = num;
|
|
seen[num] = 1;
|
|
count++;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int j = 0; j < SIZE; j++)
|
|
{
|
|
if (arr[block + i][j] == 0)
|
|
{
|
|
arr[block + i][j] = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void printarr(int arr[SIZE][SIZE])
|
|
{
|
|
int i,j,k,n;
|
|
for(n=0;n<=6;n=n+3)
|
|
{
|
|
|
|
printf("|-----------------------|\n") ;
|
|
for(i=n;i<=n+2;i++)
|
|
{
|
|
for(k=0;k<=6;k=k+3)
|
|
{
|
|
printf("|");
|
|
for(j=k;j<=k+2;j++)
|
|
{
|
|
printf(" %d",arr[i][j]);
|
|
}
|
|
printf(" ");
|
|
}
|
|
printf("|\n");
|
|
}
|
|
|
|
}
|
|
printf("|-----------------------|\n") ;
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
{
|
|
srand(time(NULL));
|
|
int arr[SIZE][SIZE] = {0};
|
|
|
|
fillArr(arr);
|
|
printarr(arr);
|
|
|
|
return 0;
|
|
}
|