#include #include #include void print_matrix(int matrix[9][9]) { int i,j; for (i = 0; i < 9; i++) { for (j = 0; j < 9; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } int main() { srand(time(NULL)); int matrix[9][9] = {0}; int i,j; for (i = 0; i < 9; i += 3) { int nums[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int flag1[9] = {0}; int flag2[9] = {0}; for(j=0;j<9;j++){ int t,p; re1: t=rand()%9; if(flag1[t]==1){ goto re1;} re2: p=rand()%9; if(flag2[p]==1){ goto re2; } matrix[i+j/3][p]=nums[t]; flag1[t]=1; flag2[p]=1; } } print_matrix(matrix); return 0; }