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.

48 lines
922 B

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
int used[10];
void row(int row[]){
int nums[3];
int numcount=0;
while(numcount < 3){
int num=rand()%9+1;
if(!used[num]){
nums[numcount++]=num;
used[num]=1;
}
}
int i;
for ( i=0;i < 3; i++) {
int index;
do{
index=rand()%9;
}while(row[index] != 0);
row[index]=nums[i];
}
}
int main(){
int i,j;
int a[10][10];
srand((unsigned)time(NULL));
for ( i = 0; i < 9; i++){
for( j=0; j < 9; j++){
a[i][j]=0;
}
if(i % 3 == 0)
memset(used, 0, sizeof(used));
row(a[i]);
}
for(i=0;i < 9;i++){
for( j=0;j < 9;j++) {
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}