2020-11-06 09:57:35 +08:00
|
|
|
---
|
|
|
|
title: 第四次作业(答案)
|
|
|
|
mathjax: true
|
|
|
|
date: 2020-11-06 09:57:17
|
|
|
|
tags:
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
# P137-3
|
|
|
|
|
|
|
|
```C
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2020-11-07 12:07:02 +08:00
|
|
|
// 输入
|
|
|
|
printf("最大公约数与最小公倍数计算");
|
|
|
|
int m, n;
|
|
|
|
printf("请输入正整数m和n:\nm = ");
|
|
|
|
if (scanf("%d", &m) != 1 || m <= 0) {
|
|
|
|
fprintf(stderr, "非法输入!\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
printf("n = ");
|
|
|
|
if (scanf("%d", &n) != 1 || n <= 0) {
|
|
|
|
fprintf(stderr, "非法输入!\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// a为较大者,b为较小者
|
|
|
|
int a, b;
|
|
|
|
if (m > n) {
|
|
|
|
a = m;
|
|
|
|
b = n;
|
|
|
|
} else {
|
|
|
|
a = n;
|
|
|
|
b = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 求最大公因数
|
|
|
|
int t = 1;
|
|
|
|
while (t != 0) {
|
|
|
|
t = a % b;
|
|
|
|
a = b;
|
|
|
|
b = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 最小公倍数
|
|
|
|
int lcm = m * n / a;
|
|
|
|
|
|
|
|
printf("最大公因数:%d\n最小公倍数:%d\n", a, lcm);
|
2020-11-06 09:57:35 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
# P137-5
|
|
|
|
|
|
|
|
$$
|
|
|
|
\begin{align*}
|
|
|
|
S_{n} &= a + aa + aaa + \cdots + \overbrace{aa \cdots a}^{n个a} \\
|
|
|
|
&= a \times 10^{n - 1} + 2a \times 10^{n - 2} + \cdots + na \\
|
|
|
|
&= (((a \times 10 + 2a) \times 10 + 3a) \times 10 + \cdots ) + na
|
|
|
|
\end{align*}
|
|
|
|
$$
|
|
|
|
|
|
|
|
由此,我们编写程序:
|
|
|
|
|
|
|
|
```C
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2020-11-07 12:07:02 +08:00
|
|
|
int a, n;
|
|
|
|
printf("P137-5\na = ");
|
|
|
|
scanf("%d", &a);
|
|
|
|
printf("n = ");
|
|
|
|
scanf("%d", &n);
|
|
|
|
|
|
|
|
int sum = a;
|
|
|
|
|
|
|
|
for (int i = 2; i <= n; ++i) {
|
|
|
|
sum *= 10;
|
|
|
|
sum += a * i;
|
|
|
|
}
|
|
|
|
printf("S = %d\n", sum);
|
|
|
|
return 0;
|
2020-11-06 09:57:35 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
# P138-14
|
|
|
|
|
|
|
|
迭代公式可表示为:
|
|
|
|
|
|
|
|
$$
|
|
|
|
\begin{equation*}
|
|
|
|
x_{n + 1} = x_{n} - \frac{f(x_{n})}{f'(x_{n})}
|
|
|
|
\end{equation*}
|
|
|
|
$$
|
|
|
|
|
|
|
|
设
|
|
|
|
|
|
|
|
$$
|
|
|
|
\begin{equation*}
|
|
|
|
f(x) = 2x^{3} - 4x^{2} + 3 x - 6
|
|
|
|
\end{equation*}
|
|
|
|
$$
|
|
|
|
|
|
|
|
则
|
|
|
|
|
|
|
|
$$
|
|
|
|
\begin{align*}
|
|
|
|
f'(x) &= 6x^{2} - 8x + 3 \\
|
|
|
|
x_{n + 1} &= x_{n} - \frac{f(x_{n})}{f'(x_{n})} \\
|
|
|
|
&= x_{n} - \frac{2x_{n}^{3} - 4x_{n}^{2} + 3x_{n} - 6}{6x_{n}^{2} - 8x_{n} + 3} \\
|
|
|
|
&= \frac{4x_{n}^{3} - 4x_{n}^{2} + 6}{6x_{n}^{2} - 8x_{n} + 3} \\
|
|
|
|
\end{align*}
|
|
|
|
$$
|
|
|
|
|
|
|
|
不妨设迭代的终止条件为:
|
|
|
|
|
|
|
|
$$
|
|
|
|
\begin{equation*}
|
|
|
|
|f(x_{n})| < 10^{-6}
|
|
|
|
\end{equation*}
|
|
|
|
$$
|
|
|
|
|
|
|
|
此时求得的 $x_{n}$ 可以作为一个近似解。
|
|
|
|
|
|
|
|
```C
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2020-11-07 12:07:02 +08:00
|
|
|
double t = 1e-6;
|
|
|
|
double x = 1.5;
|
|
|
|
while(fabs(2 * pow(x, 3) - 4 * pow(x, 2) + 3 * x - 6) >= t) {
|
|
|
|
x = (4 * pow(x, 3) - 4 * pow(x, 2) + 6) / (6 * pow(x, 2) - 8 * x + 3);
|
|
|
|
}
|
|
|
|
printf("P138-14\nx = %.3f\n", x);
|
|
|
|
return 0;
|
2020-11-06 09:57:35 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
# P138-15
|
|
|
|
|
|
|
|
```C
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2020-11-07 12:07:02 +08:00
|
|
|
double a = -10, b = 10, c = 0;
|
|
|
|
double t = 1e-6;
|
|
|
|
double fa = 2 * pow(a, 3) - 4 * pow(a, 2) + 3 * a - 6;
|
|
|
|
double fc = 2 * pow(c, 3) - 4 * pow(c, 2) + 3 * c - 6;
|
|
|
|
while (fabs(2 * pow(c, 3) - 4 * pow(c, 2) + 3 * c - 6) >= t) {
|
|
|
|
if (fa * fc < 0) {
|
|
|
|
b = c;
|
|
|
|
} else {
|
|
|
|
a = c;
|
|
|
|
fa = fc;
|
|
|
|
}
|
|
|
|
c = (a + b) / 2;
|
|
|
|
fc = 2 * pow(c, 3) - 4 * pow(c, 2) + 3 * c - 6;
|
|
|
|
}
|
|
|
|
printf("P138-15\nx = %.3f\n", c);
|
|
|
|
return 0;
|
2020-11-06 09:57:35 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
# 选择排序
|
|
|
|
|
|
|
|
```C
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#define LEN 10
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2020-11-07 12:07:02 +08:00
|
|
|
printf("请输入%d个数字,用空格分隔:\n", LEN);
|
|
|
|
int a[LEN];
|
|
|
|
for (int i = 0; i < LEN; ++i) {
|
|
|
|
scanf("%d", a + i);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < LEN - 1; ++i) {
|
|
|
|
int m = i;
|
|
|
|
for (int j = i + 1; j < LEN; ++j) {
|
|
|
|
if (a[j] < a[m]) {
|
|
|
|
m = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m != i) {
|
|
|
|
int tmp = a[i];
|
|
|
|
a[i] = a[m];
|
|
|
|
a[m] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < LEN; ++i) {
|
|
|
|
printf("%d ", a[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return 0;
|
2020-11-06 09:57:35 +08:00
|
|
|
}
|
|
|
|
```
|