diff --git a/【实践课外】6.循环结构3/7-1 二分法求多项式单根.c b/【实践课外】6.循环结构3/7-1 二分法求多项式单根.c new file mode 100644 index 0000000..6a83bd9 --- /dev/null +++ b/【实践课外】6.循环结构3/7-1 二分法求多项式单根.c @@ -0,0 +1,28 @@ +#include + +int main() { + double a3, a2, a1, a0, a, b, ans = 0; + + scanf("%lf%lf%lf%lf%lf%lf", &a3, &a2, &a1, &a0, &a, &b); + + while (b - a > 1e-4) { + double mid = (a + b) / 2; + double f_a = a3 * a * a * a + a2 * a * a + a1 * a + a0; + double f_mid = a3 * mid * mid * mid + a2 * mid * mid + a1 * mid + a0; + double f_b = a3 * b * b * b + a2 * b * b + a1 * b + a0; + + ans = mid; + + if (-1e-4 < f_mid && f_mid < 1e-4) { // 近似地认为 f(mid) == 0 + break; + } else if (f_a * f_mid > 0) { // 在 (mid, b) 中 + a = mid; + } else { // 在 (a, mid) 中 + b = mid; + } + } + + printf("%.2lf\n", ans); + + return 0; +} diff --git a/【实践课外】6.循环结构3/7-1 二分法求多项式单根.jpg b/【实践课外】6.循环结构3/7-1 二分法求多项式单根.jpg new file mode 100644 index 0000000..de1ebed Binary files /dev/null and b/【实践课外】6.循环结构3/7-1 二分法求多项式单根.jpg differ diff --git a/【实践课外】6.循环结构3/7-2 循环-十进制转化.c b/【实践课外】6.循环结构3/7-2 循环-十进制转化.c new file mode 100644 index 0000000..44293dc --- /dev/null +++ b/【实践课外】6.循环结构3/7-2 循环-十进制转化.c @@ -0,0 +1,19 @@ +#include + +int main() { + int n, m; + + scanf("%d%d", &n, &m); + + int result = 0, factor = 1; + + while (n) { + result += (n % m) * factor; + n /= m; + factor *= 10; + } + + printf("%d\n", result); + + return 0; +} diff --git a/【实践课外】6.循环结构3/7-2 循环-十进制转化.jpg b/【实践课外】6.循环结构3/7-2 循环-十进制转化.jpg new file mode 100644 index 0000000..45d2309 Binary files /dev/null and b/【实践课外】6.循环结构3/7-2 循环-十进制转化.jpg differ diff --git a/【实践课外】6.循环结构3/7-3 梅森数.c b/【实践课外】6.循环结构3/7-3 梅森数.c new file mode 100644 index 0000000..692475d --- /dev/null +++ b/【实践课外】6.循环结构3/7-3 梅森数.c @@ -0,0 +1,32 @@ +#include + +int main() { + int n, num = 2, count = 0; + + scanf("%d", &n); + + for (int i = 2; i <= n; i++) { + num *= 2; + + int is_prime = 1; + + for (int j = 2; j * j <= num - 1; j++) { + if ((num - 1) % j == 0) { + is_prime = 0; + + break; + } + } + + if (is_prime == 1) { + printf("%d\n", num - 1); + + count++; + } + } + if (count == 0) { + printf("None\n"); + } + + return 0; +} diff --git a/【实践课外】6.循环结构3/7-3 梅森数.jpg b/【实践课外】6.循环结构3/7-3 梅森数.jpg new file mode 100644 index 0000000..9cf8da5 Binary files /dev/null and b/【实践课外】6.循环结构3/7-3 梅森数.jpg differ diff --git a/【实践课外】6.循环结构3/7-4 单词长度.c b/【实践课外】6.循环结构3/7-4 单词长度.c new file mode 100644 index 0000000..0f0a4f2 --- /dev/null +++ b/【实践课外】6.循环结构3/7-4 单词长度.c @@ -0,0 +1,39 @@ +#include + +int main() { + char c; + int count = 0; + int printed = 0; + + while (scanf("%c", &c) != EOF, c != '.') { + if (c == ' ') { + if (count != 0) { + if (printed == 0) { + printf("%d", count); + + printed = 1; + } else { + printf(" %d", count); + } + } + + count = 0; + } else { + count++; + } + } + + if (count != 0) { + if (printed == 0) { + printf("%d", count); + + printed = 1; + } else { + printf(" %d", count); + } + } + + printf("\n"); + + return 0; +} diff --git a/【实践课外】6.循环结构3/7-4 单词长度.jpg b/【实践课外】6.循环结构3/7-4 单词长度.jpg new file mode 100644 index 0000000..33e59af Binary files /dev/null and b/【实践课外】6.循环结构3/7-4 单词长度.jpg differ diff --git a/【实践课外】6.循环结构3/7-5 21循环-求和3.c b/【实践课外】6.循环结构3/7-5 21循环-求和3.c new file mode 100644 index 0000000..cf7846e --- /dev/null +++ b/【实践课外】6.循环结构3/7-5 21循环-求和3.c @@ -0,0 +1,21 @@ +#include + +int main() { + int n, k, ans = 0; + + scanf("%d%d", &n, &k); + + for (int i = 1; i <= n; i++) { + int tmp = 1; + + for (int j = 1; j <= k; j++) { + tmp = tmp * i % 114514; + } + + ans = (ans + tmp) % 114514; + } + + printf("%d\n", ans); + + return 0; +} diff --git a/【实践课外】6.循环结构3/7-5 21循环-求和3.jpg b/【实践课外】6.循环结构3/7-5 21循环-求和3.jpg new file mode 100644 index 0000000..b411234 Binary files /dev/null and b/【实践课外】6.循环结构3/7-5 21循环-求和3.jpg differ diff --git a/【实践课外】6.循环结构3/7-6 21循环-金字塔.c b/【实践课外】6.循环结构3/7-6 21循环-金字塔.c new file mode 100644 index 0000000..b8faeee --- /dev/null +++ b/【实践课外】6.循环结构3/7-6 21循环-金字塔.c @@ -0,0 +1,39 @@ +#include + +int main() { + int n, sum = 0, max_level; + + scanf("%d", &n); + + // 计算可以摆出的最大金字塔层数 + for (max_level = 1; sum + max_level <= n; max_level++) { + sum += max_level; + } + + max_level--; // 循环结束时 i 多加了一次 + + // 自顶向下 + for (int j = max_level; j >= 1; j--) { + // 左侧的 0 + for (int k = j; k > 1; k--) { + printf("0"); + } + + // 当前层的数字 + printf("%d", j); + + // 中间的0和当前层的数字 + for (int m = j; m < max_level; m++) { + printf("0%d", j); + } + + // 右侧的 0 + for (int k = j; k > 1; k--) { + printf("0"); + } + + printf("\n"); + } + + return 0; +} diff --git a/【实践课外】6.循环结构3/7-6 21循环-金字塔.jpg b/【实践课外】6.循环结构3/7-6 21循环-金字塔.jpg new file mode 100644 index 0000000..9e22d09 Binary files /dev/null and b/【实践课外】6.循环结构3/7-6 21循环-金字塔.jpg differ diff --git a/【实践课外】6.循环结构3/7-7 循环-杨辉三角.c b/【实践课外】6.循环结构3/7-7 循环-杨辉三角.c new file mode 100644 index 0000000..15fe831 --- /dev/null +++ b/【实践课外】6.循环结构3/7-7 循环-杨辉三角.c @@ -0,0 +1,41 @@ +#include + +int n, m, a[25][25]; + +int main() { + scanf("%d%d", &n, &m); + + if (n > m) { + int t = n; + n = m; + m = t; + } + + // 递推法计算杨辉三角 + for (int i = 1; i <= m; i++) { + a[i][1] = a[i][i] = 1; + + for (int j = 2; j < i; j++) { + a[i][j] = a[i - 1][j - 1] + a[i - 1][j]; + } + } + + for (int i = n; i <= m; i++) { + // 输出空格 + for (int j = 1; j <= m - i; j++) { + printf(" "); + } + + for (int j = 1; j <= i; j++) { + printf("%d", a[i][j] % 10); + + if (j != i) { + printf(" "); + } + } + + printf("\n"); + } + + return 0; +} diff --git a/【实践课外】6.循环结构3/7-7 循环-杨辉三角.jpg b/【实践课外】6.循环结构3/7-7 循环-杨辉三角.jpg new file mode 100644 index 0000000..3f776fd Binary files /dev/null and b/【实践课外】6.循环结构3/7-7 循环-杨辉三角.jpg differ