Lesson 6
This commit is contained in:
1
06/01.drawio
Normal file
1
06/01.drawio
Normal file
File diff suppressed because one or more lines are too long
28
06/Exercise01.c
Normal file
28
06/Exercise01.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
|
||||
bool isPrime(int toBeTested){
|
||||
int iterationMax = 0, nowCheck = 2;
|
||||
bool isPrime = true;
|
||||
iterationMax = sqrt(toBeTested);
|
||||
do {
|
||||
if (toBeTested % nowCheck == 0) {
|
||||
isPrime = false;
|
||||
break;
|
||||
}
|
||||
nowCheck++;
|
||||
}
|
||||
while (nowCheck <= iterationMax);
|
||||
return isPrime;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int j;
|
||||
for (j = 11; j < 198; j+2) {
|
||||
if(isPrime(j) && isPrime(j+2)) {
|
||||
printf("%d %d\n", j, j+2);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
35
06/Exercise02.c
Normal file
35
06/Exercise02.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int n, astCount, spaceCount, outAstCount, outSpaceCount;
|
||||
scanf("%d", &n);
|
||||
spaceCount = (n-1)/2;
|
||||
if (n > 2 && n % 2 == 1) {
|
||||
for(astCount = 1; astCount <= n; astCount+=2){
|
||||
for (outSpaceCount = 0; outSpaceCount < spaceCount; outSpaceCount++) {
|
||||
printf(" ");
|
||||
}
|
||||
for (outAstCount = 0; outAstCount < astCount; outAstCount++) {
|
||||
printf("*");
|
||||
}
|
||||
printf("\n");
|
||||
spaceCount--;
|
||||
}
|
||||
astCount = n-2;
|
||||
spaceCount = 1;
|
||||
for(; astCount > 0; astCount-=2) {
|
||||
for (outSpaceCount = 0; outSpaceCount < spaceCount; outSpaceCount++) {
|
||||
printf(" ");
|
||||
}
|
||||
for (outAstCount = 0; outAstCount < astCount; outAstCount++) {
|
||||
printf("*");
|
||||
}
|
||||
printf("\n");
|
||||
spaceCount++;
|
||||
}
|
||||
}
|
||||
else{
|
||||
printf("input error");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
35
06/Exercise03.c
Normal file
35
06/Exercise03.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
|
||||
bool isPrime(int toBeTested){
|
||||
int iterationMax = 0, nowCheck = 2;
|
||||
bool isPrime = true;
|
||||
iterationMax = sqrt(toBeTested);
|
||||
do {
|
||||
if (toBeTested % nowCheck == 0) {
|
||||
isPrime = false;
|
||||
break;
|
||||
}
|
||||
nowCheck++;
|
||||
}
|
||||
while (nowCheck <= iterationMax);
|
||||
return isPrime;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n, p1;
|
||||
scanf("%d", &n);
|
||||
if (n < 4 || n % 2 == 1) {
|
||||
printf("input error");
|
||||
}
|
||||
else {
|
||||
for (p1=3;;p1+=2) {
|
||||
if (isPrime(p1) && isPrime(n - p1)) {
|
||||
printf("%d = %d + %d", n, p1, n-p1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
12
06/Optional01.c
Normal file
12
06/Optional01.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int apple[6] = {0};
|
||||
int childCount = 4;
|
||||
apple[5] = 11;
|
||||
for (;childCount > 0; childCount--) {
|
||||
apple[childCount] = (apple[childCount+1] * (childCount + 1) + 1)/childCount;
|
||||
}
|
||||
printf("Total number of apple: %d\n 1: %d\n 2: %d\n 3: %d\n 4: %d\n 5: %d\n", apple[1], apple[1]-apple[2], apple[2]-apple[3], apple[3]-apple[4], apple[4]-apple[5], apple[5]);
|
||||
return 0;
|
||||
}
|
||||
21
06/Optional02.c
Normal file
21
06/Optional02.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int factorials[10] = {0};
|
||||
|
||||
int factorial(int n) {
|
||||
if (factorials[n-1] == 0) {
|
||||
factorials[n-1] = n * factorial(n-1);
|
||||
}
|
||||
return factorials[n-1];
|
||||
}
|
||||
|
||||
int main() {
|
||||
int k = 0, sum = 0, i;
|
||||
scanf("%d", &k);
|
||||
factorials[0] = 1;
|
||||
for (i = 1;i <= k; i++) {
|
||||
sum += factorial(i);
|
||||
}
|
||||
printf("%d", sum);
|
||||
return 0;
|
||||
}
|
||||
37
06/Optional03.c
Normal file
37
06/Optional03.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int root = 2;// 若一个数有奇数个因子,那么它一定是平方数
|
||||
int possibleFactor = 0;
|
||||
int factorCount = 0;
|
||||
int factors[5] = {0};
|
||||
int testee = 0;
|
||||
factors[0] = 1;
|
||||
printf("1~1000内只包含5个因子的数是:\n");
|
||||
for (;root < 32; root++) {
|
||||
//31^2 < 1000 ^ 32^2,只用考虑到31
|
||||
testee = root*root;
|
||||
factorCount = 0;
|
||||
|
||||
factors[2] = root;
|
||||
factors[4] = testee;
|
||||
|
||||
for (possibleFactor = 2; (possibleFactor < root); possibleFactor++) {
|
||||
if (testee % possibleFactor == 0) {
|
||||
if (factorCount == 0) {
|
||||
factors[1] = possibleFactor;
|
||||
factors[3] = testee / possibleFactor;
|
||||
factorCount++;
|
||||
}
|
||||
else {
|
||||
factorCount++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (factorCount == 1) {
|
||||
printf("%3d: %4d%4d%4d%4d%4d\n",testee, factors[0], factors[1], factors[2], factors[3], factors[4]);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
35
06/Optional04.c
Normal file
35
06/Optional04.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int hats[6] = {0};
|
||||
int count = 0;
|
||||
|
||||
void nextChild(int childIndex, int whiteRemaining, int blackremaining) {
|
||||
int i;
|
||||
if (childIndex == 6) {
|
||||
count++;
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (hats[i] == -1) {
|
||||
printf("White");
|
||||
}
|
||||
else {
|
||||
printf("Black");
|
||||
}
|
||||
printf("\t");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
if (whiteRemaining > 0) {
|
||||
hats[childIndex] = -1;
|
||||
nextChild(childIndex + 1, whiteRemaining - 1, blackremaining);
|
||||
}
|
||||
if (blackremaining > 0) {
|
||||
hats[childIndex] = 1;
|
||||
nextChild(childIndex + 1, whiteRemaining, blackremaining - 1);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
nextChild(0, 3, 3);
|
||||
printf("Total: %d", count);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user