1
0
mirror of https://github.com/renbaoshuo/202401-programming-assignments.git synced 2024-11-23 09:38:42 +00:00

【实践课外】6.循环结构3

This commit is contained in:
Baoshuo Ren 2024-11-02 20:58:43 +08:00
parent 889f44af50
commit 01f72aa8a0
Failed to extract signature
14 changed files with 219 additions and 0 deletions

View File

@ -0,0 +1,28 @@
#include <stdio.h>
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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 427 KiB

View File

@ -0,0 +1,19 @@
#include <stdio.h>
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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 324 KiB

View File

@ -0,0 +1,32 @@
#include <stdio.h>
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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 323 KiB

View File

@ -0,0 +1,39 @@
#include <stdio.h>
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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 325 KiB

View File

@ -0,0 +1,21 @@
#include <stdio.h>
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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 308 KiB

View File

@ -0,0 +1,39 @@
#include <stdio.h>
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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 346 KiB

View File

@ -0,0 +1,41 @@
#include <stdio.h>
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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 358 KiB