鱼C论坛

 找回密码
 立即注册
查看: 2465|回复: 2

[已解决]C语言编程题02

[复制链接]
发表于 2023-12-27 17:12:17 | 显示全部楼层 |阅读模式
5鱼币
本帖最后由 liyifeng295641 于 2024-1-3 15:45 编辑

1. 输入一元二次方程的三个系数 a,b,c,求出方程的根,如果没有跟显示 Error。

2. 把一个整数数组改为逆序,并将结果输出,例如:原数组a[N]={1,2,3,4,5},变为a[N]={5,4,3,2,1}。

3. 从键盘上输入若干个整数,其值在 0-9 之间,用-1 作为结束输入的标记,统计 0-9 中各个数
的个数,并输出。
最佳答案
2023-12-27 17:12:18
这样行吗?

  1. #include <stdio.h>
  2. #include <math.h>

  3. // 一元二次方程求解
  4. void solve_quadratic_equation() {
  5.     double a, b, c, determinant, root1, root2;

  6.     printf("Enter coefficients a, b and c: ");
  7.     scanf("%lf %lf %lf", &a, &b, &c);

  8.     determinant = b*b - 4*a*c;

  9.     if (determinant > 0) {
  10.         root1 = (-b + sqrt(determinant)) / (2*a);
  11.         root2 = (-b - sqrt(determinant)) / (2*a);
  12.         printf("Roots are real and different:\n%.2lf\n%.2lf\n", root1, root2);
  13.     }
  14.     else if (determinant == 0) {
  15.         root1 = -b/(2*a);
  16.         printf("Roots are real and same:\n%.2lf\n", root1);
  17.     }
  18.     else {
  19.         printf("Error\n");
  20.     }
  21. }

  22. // 逆序整数数组
  23. void reverse_array() {
  24.     int n, i;

  25.     printf("Enter the number of elements: ");
  26.     scanf("%d", &n);
  27.     int arr[n];
  28.     printf("Enter %d elements:\n", n);
  29.     for(i = 0; i < n; i++) {
  30.         scanf("%d", &arr[i]);
  31.     }

  32.     printf("Reversed array: ");
  33.     for(i = n - 1; i >= 0; i--) {
  34.         printf("%d ", arr[i]);
  35.     }
  36.     printf("\n");
  37. }

  38. // 统计数字次数
  39. void count_numbers() {
  40.     int num, count[10] = {0};

  41.     printf("Enter numbers between 0 and 9 (enter -1 to end): ");
  42.     while(1) {
  43.         scanf("%d", &num);
  44.         if(num == -1)
  45.             break;
  46.         if(num >= 0 && num < 10)
  47.             count[num]++;
  48.     }

  49.     printf("Count of numbers:\n");
  50.     for(int i = 0; i < 10; i++) {
  51.         printf("%d: %d\n", i, count[i]);
  52.     }
  53. }

  54. int main() {
  55.     solve_quadratic_equation();
  56.     reverse_array();
  57.     count_numbers();
  58.     return 0;
  59. }
复制代码

最佳答案

查看完整内容

这样行吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-12-27 17:12:18 | 显示全部楼层    本楼为最佳答案   
这样行吗?

  1. #include <stdio.h>
  2. #include <math.h>

  3. // 一元二次方程求解
  4. void solve_quadratic_equation() {
  5.     double a, b, c, determinant, root1, root2;

  6.     printf("Enter coefficients a, b and c: ");
  7.     scanf("%lf %lf %lf", &a, &b, &c);

  8.     determinant = b*b - 4*a*c;

  9.     if (determinant > 0) {
  10.         root1 = (-b + sqrt(determinant)) / (2*a);
  11.         root2 = (-b - sqrt(determinant)) / (2*a);
  12.         printf("Roots are real and different:\n%.2lf\n%.2lf\n", root1, root2);
  13.     }
  14.     else if (determinant == 0) {
  15.         root1 = -b/(2*a);
  16.         printf("Roots are real and same:\n%.2lf\n", root1);
  17.     }
  18.     else {
  19.         printf("Error\n");
  20.     }
  21. }

  22. // 逆序整数数组
  23. void reverse_array() {
  24.     int n, i;

  25.     printf("Enter the number of elements: ");
  26.     scanf("%d", &n);
  27.     int arr[n];
  28.     printf("Enter %d elements:\n", n);
  29.     for(i = 0; i < n; i++) {
  30.         scanf("%d", &arr[i]);
  31.     }

  32.     printf("Reversed array: ");
  33.     for(i = n - 1; i >= 0; i--) {
  34.         printf("%d ", arr[i]);
  35.     }
  36.     printf("\n");
  37. }

  38. // 统计数字次数
  39. void count_numbers() {
  40.     int num, count[10] = {0};

  41.     printf("Enter numbers between 0 and 9 (enter -1 to end): ");
  42.     while(1) {
  43.         scanf("%d", &num);
  44.         if(num == -1)
  45.             break;
  46.         if(num >= 0 && num < 10)
  47.             count[num]++;
  48.     }

  49.     printf("Count of numbers:\n");
  50.     for(int i = 0; i < 10; i++) {
  51.         printf("%d: %d\n", i, count[i]);
  52.     }
  53. }

  54. int main() {
  55.     solve_quadratic_equation();
  56.     reverse_array();
  57.     count_numbers();
  58.     return 0;
  59. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-12-27 17:12:45 | 显示全部楼层
1.题目要求求一元二次方程的根,可以使用一元二次方程的求根公式来解决。如果根不存在,则输出Error。
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main() {
  4.     double a, b, c;
  5.     double x1, x2;
  6.    
  7.     printf("请输入一元二次方程的三个系数(a, b, c):");
  8.     scanf("%lf %lf %lf", &a, &b, &c);
  9.    
  10.     double discriminant = b * b - 4 * a * c;
  11.    
  12.     if (a == 0) {
  13.         x1 = -c / b;
  14.         printf("方程的根为:%f\n", x1);
  15.     } else if (discriminant > 0) {
  16.         x1 = (-b + sqrt(discriminant)) / (2 * a);
  17.         x2 = (-b - sqrt(discriminant)) / (2 * a);
  18.         printf("方程的根为:%f, %f\n", x1, x2);
  19.     } else if (discriminant == 0) {
  20.         x1 = -b / (2 * a);
  21.         printf("方程的根为:%f\n", x1);
  22.     } else {
  23.         printf("Error: 方程无根\n");
  24.     }
  25.    
  26.     return 0;
  27. }
复制代码

2.题目要求将整数数组改为逆序。可以定义两个指针,一个指向数组的起始位置,另一个指向数组的末尾位置,然后将两个指针所指的元素交换位置,直到两个指针相遇。
  1. #include <stdio.h>
  2. void reverseArray(int arr[], int length) {
  3.     int *start = arr;
  4.     int *end = arr + length - 1;
  5.    
  6.     while (start < end) {
  7.         int temp = *start;
  8.         *start = *end;
  9.         *end = temp;
  10.         
  11.         start++;
  12.         end--;
  13.     }
  14. }
  15. int main() {
  16.     int arr[] = {1, 2, 3, 4, 5};
  17.     int length = sizeof(arr) / sizeof(arr[0]);
  18.    
  19.     // 输出原数组
  20.     printf("原数组:");
  21.     for (int i = 0; i < length; i++) {
  22.         printf("%d ", arr[i]);
  23.     }
  24.     printf("\n");
  25.    
  26.     reverseArray(arr, length);
  27.    
  28.     // 输出逆序后的数组
  29.     printf("逆序后的数组:");
  30.     for (int i = 0; i < length; i++) {
  31.         printf("%d ", arr[i]);
  32.     }
  33.     printf("\n");
  34.    
  35.     return 0;
  36. }
复制代码

3.题目要求统计键盘输入的一系列整数中0-9每个数字出现的次数。可以使用一个大小为10的数组来保存各个数字出现的次数,然后根据用户输入的整数进行相应的计数。
  1. #include <stdio.h>
  2. int main() {
  3.     int count[10] = {0}; // 初始化计数数组,全部置为0
  4.     int num;
  5.    
  6.     printf("请输入若干个整数(0-9之间),以-1结束:\n");
  7.    
  8.     do {
  9.         scanf("%d", &num);
  10.         
  11.         if (num >= 0 && num <= 9) {
  12.             count[num]++;
  13.         }
  14.     } while (num != -1);
  15.    
  16.     printf("各个数字出现的次数如下:\n");
  17.     for (int i = 0; i < 10; i++) {
  18.         printf("%d: %d次\n", i, count[i]);
  19.     }
  20.    
  21.     return 0;
  22. }
复制代码


以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-8 10:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表