鱼C论坛

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

C语言数组问题

[复制链接]
发表于 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
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-20 03:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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