#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int a[9],b[9]={1,2,3,4,5,6,7,8,9};
int c[9];
//设计思想是从t位置开始,重新设计数组,t~9保存在一个数组的前面部分,1~t-1保存在后面部分,这样来构成一个序列,并且只要首元素不重复,这个序列的每一位就不会跟上面的序列重复。t有两种来源,一种是因为刚好当前行数%3 == 0时
void order_result(const int b[],int c[])
{
//srand((unsigned int)time(NULL));
int t;//用来保存一个临时变量
int i,j=0;
t =rand()%9+1;
for(i = t; i < 9; i++)
{
c[j] = b[i];
j++;
}
for( i =0 ; i < t ; i++)
{
c[j] = b[i];
j++;
}
}
void order_result_2(const int b[],int c[],int t)//指定从t的位置出发开始考虑
{
int i,j=0;
for(i = t; i < 9; i++)
{
c[j] = b[i];
j++;
}
for( i =0 ; i < t ; i++)
{
c[j] = b[i];
j++;
}
}
void print_result(int b[])
{
int i,j;
int tmp;
for(i = 0; i < 9; i++)
{
if(i%3 == 0)
{
j = 0;
order_result(b,c);
while(j < i)
{
if(a[j] != a[i]) j++;
else
{
order_result(b,c);
a[i] =c[0];
j = 0;
}
}
}
else
{
order_result_2(b,c,c[2]);
a[i] =c[0];
}
for(j = 0; j < 9 ; j++)
{
tmp =rand()%9+1;
if(tmp>=6)
printf("%d\t",c[j]);
else printf("\t");
}
printf("\n");
}
}
void main()
{
srand((unsigned int)time(NULL));//随即化
print_result(b);
}
利用随机数编写的一个数独
最新推荐文章于 2024-06-16 20:39:01 发布