鱼C论坛

 找回密码
 立即注册
查看: 1242|回复: 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 可以的话,试试自己做吧:
#include <iostream>
#include <algorithm>
#include <sstream>
#include <vector>
#include <string>
#include <iomanip>
#include <cmath>

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

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

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

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

int main() {
    std::string str;
    std::getline(std::cin, str);
    std::stringstream ss(str);
    auto start = std::istream_iterator<int>{ ss };
    auto end = std::istream_iterator<int>{};
    std::vector<int> score(start, end);
    std::cout
        << min(score) << " " // 最小值
        << max(score) << " " // 最大值
        << std::fixed
        << std::setprecision(1)
        << avg(score) << " " // 平均值
        << std::setprecision(2)
        << MSE(score) // 均方差
        << std::endl;
        return 0;
}
80 82 84 86
80 86 83.0 2.24

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

使用道具 举报

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


int main()
{
        int n=0,i=0;
        float a=0,sumpow2=0,sum=0,avrg=0,max=0,min=100;
        scanf("%d",&n);
        i=n;
        while (i--)
        {
                scanf("%f",&a);
                if (a>max)max=a;
                if(a<min)min=a;
                sumpow2+=a*a;
                sum+=a;
        }
        avrg=sum/n;
        printf("min=%.1f,max=%.1f,average=%.1f,square deviation=%.2f",\
                min,max,avrg,sqrtf((sumpow2+n*avrg*avrg-2*avrg*sum)/n));


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

使用道具 举报

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-5 12:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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