缩进改用空格;修复错误

This commit is contained in:
zhbaor 2020-11-07 12:07:02 +08:00
parent 8051c780e3
commit 502434e49a

View file

@ -14,42 +14,42 @@ tags:
int main() int main()
{ {
// 输入 // 输入
printf("最大公约数与最小公倍数计算"); printf("最大公约数与最小公倍数计算");
int m, n; int m, n;
printf("请输入正整数m和n:\nm = "); printf("请输入正整数m和n:\nm = ");
if (scanf("%d", &m) != 1 || m <= 0) { if (scanf("%d", &m) != 1 || m <= 0) {
fprintf(stderr, "非法输入!\n"); fprintf(stderr, "非法输入!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("n = "); printf("n = ");
if (scanf("%d", &n) != 1 || n <= 0) { if (scanf("%d", &n) != 1 || n <= 0) {
fprintf(stderr, "非法输入!\n"); fprintf(stderr, "非法输入!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// a为较大者,b为较小者 // a为较大者,b为较小者
int a, b; int a, b;
if (m > n) { if (m > n) {
a = m; a = m;
b = n; b = n;
} else { } else {
a = n; a = n;
b = m; b = m;
} }
// 求最大公因数 // 求最大公因数
int t = 1; int t = 1;
while (t != 0) { while (t != 0) {
t = a % b; t = a % b;
a = b; a = b;
b = t; b = t;
} }
// 最小公倍数 // 最小公倍数
int lcm = m * n / a; int lcm = m * n / a;
printf("最大公因数:%d\n最小公倍数:%d\n", a, lcm); printf("最大公因数:%d\n最小公倍数:%d\n", a, lcm);
return 0; return 0;
} }
``` ```
@ -71,20 +71,20 @@ $$
int main() int main()
{ {
int a, n; int a, n;
printf("P137-5\na = "); printf("P137-5\na = ");
scanf("%d", &a); scanf("%d", &a);
printf("n = "); printf("n = ");
scanf("%d", &n); scanf("%d", &n);
int sum = a; int sum = a;
for (int i = 2; i <= n; ++i) { for (int i = 2; i <= n; ++i) {
sum *= 10; sum *= 10;
sum += a * i; sum += a * i;
} }
printf("S = %d\n", sum); printf("S = %d\n", sum);
return 0; return 0;
} }
``` ```
@ -133,13 +133,13 @@ $$
int main() int main()
{ {
double t = 1e-6; double t = 1e-6;
double x = 1.5; double x = 1.5;
while(fabs(2 * pow(x, 3) - 4 * pow(x, 2) + 3 * x - 6) >= t) { 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); x = (4 * pow(x, 3) - 4 * pow(x, 2) + 6) / (6 * pow(x, 2) - 8 * x + 3);
} }
printf("P138-14\nx = %.3f\n", x); printf("P138-14\nx = %.3f\n", x);
return 0; return 0;
} }
``` ```
@ -151,25 +151,22 @@ int main()
int main() int main()
{ {
double a = -10, b = 10, c = 0; double a = -10, b = 10, c = 0;
double t = 1e-6; double t = 1e-6;
double fa = 2 * pow(a, 3) - 4 * pow(a, 2) + 3 * a - 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; 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) { while (fabs(2 * pow(c, 3) - 4 * pow(c, 2) + 3 * c - 6) >= t) {
if (fabs(fc) < t) { if (fa * fc < 0) {
break; b = c;
} } else {
if (fa * fc < 0) { a = c;
b = c; fa = fc;
} else { }
a = c; c = (a + b) / 2;
fa = fc; fc = 2 * pow(c, 3) - 4 * pow(c, 2) + 3 * c - 6;
} }
c = (a + b) / 2; printf("P138-15\nx = %.3f\n", c);
fc = 2 * pow(c, 3) - 4 * pow(c, 2) + 3 * c - 6; return 0;
}
printf("P138-15\nx = %.3f\n", c);
return 0;
} }
``` ```
@ -183,28 +180,28 @@ int main()
int main() int main()
{ {
printf("请输入%d个数字,用空格分隔:\n", LEN); printf("请输入%d个数字,用空格分隔:\n", LEN);
int a[LEN]; int a[LEN];
for (int i = 0; i < LEN; ++i) { for (int i = 0; i < LEN; ++i) {
scanf("%d", a + i); scanf("%d", a + i);
} }
for (int i = 0; i < LEN - 1; ++i) { for (int i = 0; i < LEN - 1; ++i) {
int m = i; int m = i;
for (int j = i + 1; j < LEN; ++j) { for (int j = i + 1; j < LEN; ++j) {
if (a[j] < a[m]) { if (a[j] < a[m]) {
m = j; m = j;
} }
} }
if (m != i) { if (m != i) {
int tmp = a[i]; int tmp = a[i];
a[i] = a[m]; a[i] = a[m];
a[m] = tmp; a[m] = tmp;
} }
} }
for (int i = 0; i < LEN; ++i) { for (int i = 0; i < LEN; ++i) {
printf("%d ", a[i]); printf("%d ", a[i]);
} }
printf("\n"); printf("\n");
return 0; return 0;
} }
``` ```