鱼C论坛

 找回密码
 立即注册
查看: 1129|回复: 4

C语言数组问题

[复制链接]
发表于 2022-3-13 19:09:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目描述
输入一组成绩(整数,介于[0,100]之间,至少1个,但不多于20个),计算并输出这组成绩的最小值、最大值、平均值(一位小数)与均方差(两位小数),数据之间空一格

注:均方差的计算公式为:   ,其中μ为数据的平均值

均方差能够体现各数据项与平均值的偏差程度

例如,一组学生的成绩:80、82、84、86,平均值为83.0
方差D=[(80-83)2+(82-83)2+(84-83)2+(86-83)2]/4=5,均方差σ=2.24(方差D的平方根)

而另一组学生的成绩:82、82、83、85,平均值也为83.0
但是方差D=[(82-83)2+(82-83)2+(83-83)2+(85-83)2]/4=1.5,均方差σ=1.22
说明这一组学生的成绩比较稳定,与平均值的偏差较小
输入

输出

样例输入 Copy
80 82 84 86

82 82 83 85
样例输出 Copy
80 86 83.0 2.24

82 85 83.0 1.22
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-13 19:12:05 | 显示全部楼层
是不会做吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-13 21:01:45 | 显示全部楼层
题目已经提示非常清楚了,我写了 C++,至於 C 可以的话,试试自己做吧:
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <sstream>
  4. #include <vector>
  5. #include <string>
  6. #include <iomanip>
  7. #include <cmath>

  8. auto min = [](std::vector<int> V) -> int
  9. { return *std::min_element(V.begin(), V.end()); };

  10. auto max = [](std::vector<int> V) -> int
  11. { return *std::max_element(V.begin(), V.end()); };

  12. double avg(std::vector<int> V) {
  13.     double res = 0;
  14.     for (int x : V) res += x;
  15.     return res / static_cast<double> (V.size());
  16. }

  17. double MSE(std::vector<int> V) {
  18.     double a = avg(V);
  19.     double res = 0;
  20.     for (int x : V) res += (x - a) * (x - a);
  21.     return sqrt(res / static_cast<double> (V.size()));
  22. }

  23. int main() {
  24.     std::string str;
  25.     std::getline(std::cin, str);
  26.     std::stringstream ss(str);
  27.     auto start = std::istream_iterator<int>{ ss };
  28.     auto end = std::istream_iterator<int>{};
  29.     std::vector<int> score(start, end);
  30.     std::cout
  31.         << min(score) << " " // 最小值
  32.         << max(score) << " " // 最大值
  33.         << std::fixed
  34.         << std::setprecision(1)
  35.         << avg(score) << " " // 平均值
  36.         << std::setprecision(2)
  37.         << MSE(score) // 均方差
  38.         << std::endl;
  39.         return 0;
  40. }
复制代码
  1. 80 82 84 86
  2. 80 86 83.0 2.24

  3. 82 82 83 85
  4. 82 85 83.0 1.22
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-14 23:10:00 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <math.h>


  3. int main()
  4. {
  5.         int n=0,i=0;
  6.         float a=0,sumpow2=0,sum=0,avrg=0,max=0,min=100;
  7.         scanf("%d",&n);
  8.         i=n;
  9.         while (i--)
  10.         {
  11.                 scanf("%f",&a);
  12.                 if (a>max)max=a;
  13.                 if(a<min)min=a;
  14.                 sumpow2+=a*a;
  15.                 sum+=a;
  16.         }
  17.         avrg=sum/n;
  18.         printf("min=%.1f,max=%.1f,average=%.1f,square deviation=%.2f",\
  19.                 min,max,avrg,sqrtf((sumpow2+n*avrg*avrg-2*avrg*sum)/n));


  20.     return 0;
  21. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-15 11:07:46 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-21 23:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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