diff --git a/source/_posts/answer4.md b/source/_posts/answer4.md index 19e69fc..5aacbc4 100644 --- a/source/_posts/answer4.md +++ b/source/_posts/answer4.md @@ -14,42 +14,42 @@ tags: int main() { - // 输入 - 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); - } + // 输入 + 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; - } + // 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 t = 1; + while (t != 0) { + t = a % b; + a = b; + 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; } ``` @@ -71,20 +71,20 @@ $$ int main() { - int a, n; - printf("P137-5\na = "); - scanf("%d", &a); - printf("n = "); - scanf("%d", &n); + int a, n; + printf("P137-5\na = "); + scanf("%d", &a); + printf("n = "); + scanf("%d", &n); - int sum = a; + int sum = a; - for (int i = 2; i <= n; ++i) { - sum *= 10; - sum += a * i; - } - printf("S = %d\n", sum); - return 0; + for (int i = 2; i <= n; ++i) { + sum *= 10; + sum += a * i; + } + printf("S = %d\n", sum); + return 0; } ``` @@ -133,13 +133,13 @@ $$ int main() { - 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; + 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; } ``` @@ -151,25 +151,22 @@ int main() int main() { - 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 (fabs(fc) < t) { - break; - } - 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; + 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; } ``` @@ -183,28 +180,28 @@ int main() int main() { - 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; + 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; } ```