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.
70 lines
1.2 KiB
70 lines
1.2 KiB
1 year ago
|
#include<stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <time.h>
|
||
|
void shuffleArray(int array[], int length)
|
||
|
{
|
||
|
srand(time(NULL));
|
||
|
for (int i = length - 1; i > 0; i--)
|
||
|
{
|
||
|
int j = rand() % (i + 1);
|
||
|
int temp = array[i];
|
||
|
array[i] = array[j];
|
||
|
array[j] = temp;
|
||
|
}
|
||
|
}
|
||
|
void output(int arry[9][9])
|
||
|
{
|
||
|
int i,j;
|
||
|
printf("|-----------------------|\n");
|
||
|
for(i=0;i<9;i++)
|
||
|
{
|
||
|
for(j=0;j<9;j++)
|
||
|
{
|
||
|
if(j==0)
|
||
|
printf("| ");
|
||
|
printf("%d ",arry[i][j]);
|
||
|
if((j+1)%3==0)
|
||
|
{
|
||
|
printf("| ");
|
||
|
}
|
||
|
}
|
||
|
printf("\n");
|
||
|
if((i+1)%3==0)
|
||
|
{
|
||
|
printf("|-----------------------|\n");
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
int main()
|
||
|
{
|
||
|
void output(int arry[9][9]);
|
||
|
srand(time(NULL));
|
||
|
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||
|
int length = sizeof(array) / sizeof(array[0]);
|
||
|
shuffleArray(array, length);
|
||
|
int board[9][9]={0},number=0;
|
||
|
for(int row=0;row<9;row++)
|
||
|
{
|
||
|
int k=1;
|
||
|
while(k<4)
|
||
|
{
|
||
|
int col=rand()%9;
|
||
|
if(board[row][col]==0)
|
||
|
{
|
||
|
board[row][col]=array[number];
|
||
|
number++;
|
||
|
k++;
|
||
|
}
|
||
|
}
|
||
|
if((row+1)%3==0)
|
||
|
{
|
||
|
number=0;
|
||
|
shuffleArray(array, length);
|
||
|
}
|
||
|
}
|
||
|
output(board);
|
||
|
}
|
||
|
|