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.
ZZMDSDJZ/数独矩阵第二关.c

60 lines
1.2 KiB

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void print(int a[9][9]) {
printf("|-----------------------|\n");
int i,j;
for(i=0;i<9;i++){
printf("| ");
for(j=0;j<9;j++){
printf("%d ",a[i][j]);
if(j==2||j==5) printf("| ");
if(j==8) printf("|\n");
}
if(i==2||i==5) {
int x=22;
printf("|");
while(x>=0) {
printf("-");
x--;
}
printf("|\n");
}
}
printf("|-----------------------|\n");
}
int main() {
srand((unsigned int)time(NULL));
int a[9][9];
int i,j;
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
a[i][j] = 0;
}
}
int nums1_9[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int used[9] = {0};
for(j=0;j<9;j++){
if(j==3||j==6) {
for(i=0;i<9;i++) used[i]=0;
}
//printf("%d",j);
for (i = 0; i < 3; i++) {
int pos, num;
do {
pos = rand() % 9;
} while (a[j][pos]!= 0);
do {
num = rand() % 9;
} while (used[num]);
//printf("%d %d\n",pos,num);
a[j][pos] = nums1_9[num];
used[num] = 1;
}
}
print(a);
return 0;
}