第七课选做题。
This commit is contained in:
12
07/Optional01.c
Normal file
12
07/Optional01.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int main() {
|
||||
int i = 1;
|
||||
double count = 0;
|
||||
for (; i < 1001; i++) {
|
||||
count += log10(i);
|
||||
}
|
||||
printf("1000! has %d digits.", ((int)count) + 1);
|
||||
return 0;
|
||||
}
|
||||
14
07/Optional02.c
Normal file
14
07/Optional02.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int a, b, n, count=0;
|
||||
printf("Please input a, b, n: ");
|
||||
scanf("%d %d %d", &a, &b, &n);
|
||||
for (;n>0;n--) {
|
||||
count += a*b;
|
||||
a--;
|
||||
b--;
|
||||
}
|
||||
printf("The total number is %d", count);
|
||||
return 0;
|
||||
}
|
||||
60
07/Optional03.c
Executable file
60
07/Optional03.c
Executable file
@@ -0,0 +1,60 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
bool digits[10] = { 0 };
|
||||
|
||||
bool checkDigit(int origin, int toBeChecked) {
|
||||
bool originDigit[10] = { 0 }, toBeCheckedDigit[10] = { 0 };
|
||||
int i = 0;
|
||||
while (toBeChecked > 0) {
|
||||
if (toBeCheckedDigit[toBeChecked % 10] == true) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
toBeCheckedDigit[toBeChecked % 10] = true;
|
||||
}
|
||||
toBeChecked /= 10;
|
||||
}
|
||||
while (origin > 0) {
|
||||
originDigit[origin % 10] = true;
|
||||
origin /= 10;
|
||||
}
|
||||
while (i < 10) {
|
||||
if (originDigit[i] != toBeCheckedDigit[i]) {
|
||||
return false;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void pickDigit(int layer, int picked) {
|
||||
int i = 0;
|
||||
if (layer == 7) {
|
||||
// printf("%d\n", picked);
|
||||
if (checkDigit(picked, 2 * picked) && checkDigit(picked, 3 * picked)) {
|
||||
printf("%d\n", picked);
|
||||
}
|
||||
}
|
||||
else if (layer == 1) {
|
||||
for (i = 1; i <= 3; i++) {
|
||||
digits[i] = true;
|
||||
pickDigit(2, i);
|
||||
digits[i] = false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (i = 0; i <= 9; i++) {
|
||||
if (digits[i] == false) {
|
||||
digits[i] = true;
|
||||
pickDigit(layer + 1, picked * 10 + i);
|
||||
digits[i] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
pickDigit(1, 0);
|
||||
return 0;
|
||||
}
|
||||
42
07/Optional04.c
Executable file
42
07/Optional04.c
Executable file
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int pickedColor[6] = { 0 };
|
||||
|
||||
bool pickNextColor(int layer, int maxColorCount) {
|
||||
int i = 1;
|
||||
if (layer == 6) {
|
||||
if ((pickedColor[1] != pickedColor[5])
|
||||
&& (pickedColor[1] != pickedColor[4])
|
||||
&& (pickedColor[1] != pickedColor[3])
|
||||
&& (pickedColor[5] != pickedColor[4])
|
||||
&& (pickedColor[3] != pickedColor[4])
|
||||
&& (pickedColor[4] != pickedColor[2])
|
||||
&& (pickedColor[3] != pickedColor[2])) {
|
||||
printf("Min color required: %d\n", maxColorCount);
|
||||
printf("A: %d\nB: %d\nC: %d\nD: %d\nE: %d\n",
|
||||
pickedColor[1], pickedColor[2], pickedColor[3], pickedColor[4], pickedColor[5]);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (i = 1; i <= maxColorCount; i++) {
|
||||
pickedColor[layer] = i;
|
||||
if (pickNextColor(layer + 1, maxColorCount)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int maxColorCount = 2;
|
||||
while (!pickNextColor(1, maxColorCount)) {
|
||||
maxColorCount++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
55
07/Optional05.c
Executable file
55
07/Optional05.c
Executable 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;
|
||||
}
|
||||
Reference in New Issue
Block a user