第七课选做题。

This commit is contained in:
unlockable
2022-11-02 19:44:17 +08:00
parent 28898732bc
commit e5b23334fe
5 changed files with 183 additions and 0 deletions

55
07/Optional05.c Executable file
View File

@@ -0,0 +1,55 @@
#include <stdio.h>
#include <stdbool.h>
int numbers[10] = { 0 };
int cube[3][3] = { 0 };
void output() {
int i = 0, j = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d", cube[i][j]);
printf(" ");
}
printf("\n");
}
printf("\n");
}
void pickNext(int row, int col) {
int i = 0;
if (row == 2 && col == 2) {
for (int i = 1; i <= 9; i++) {
if (numbers[i] == false) {
cube[2][2] = i;
break;
}
}
if ((cube[0][0] + cube[0][1] + cube[0][2] == 15)
&& (cube[1][0] + cube[1][1] + cube[1][2] == 15)
&& (cube[0][0] + cube[1][0] + cube[2][0] == 15)
&& (cube[0][1] + cube[1][1] + cube[2][1] == 15)
&& (cube[0][0] + cube[1][1] + cube[2][2] == 15)
&& (cube[0][2] + cube[1][1] + cube[2][0] == 15)) {
output();
}
}
for (i = 1; i <= 9; i++) {
if (numbers[i] == false) {
numbers[i] = true;
cube[row][col] = i;
if (col == 2) {
pickNext(row + 1, 0);
}
else {
pickNext(row, col + 1);
}
numbers[i] = false;
}
}
}
int main() {
pickNext(0, 0);
return 0;
}