blaze_42550w @ 2024-12-07 16:55:05
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
double a[10000],s=0;
cin>>n;
double min=100000000;
double max=0;
for(int i=1;i<=n;i++)//输入
{
cin>>a[i];
if(a[i]>max)//最大
{
max=a[i];
}
if(a[i]<min)//最小
{
min=a[i];
}
}
for(int i=1;i<=n;i++)//总数
{
if(a[i]!=max)
{
if(a[i]!=min)
{
s+=a[i];
}
}
}
s=s/(n-2);//平均值
printf("%.2lf",s);
double dd=(-1);
for(int i=1;i<=n;i++)//最大误差
{
if(a[i]!=max)
{
if(a[i]!=min)
{
if(a[i]>s)//大减小
{
if((a[i]-s)>=dd)
{
dd=(a[i]-s);
}
}
else
{
if((s-a[i])>=dd)
{
dd=(s-a[i]);
}
}
}
}
}
cout<<" ";
printf("%.2lf",dd);
return 0;
}
by What_is_Solution @ 2024-12-07 17:24:16
错的有点多,emm...自己写了一个,看看吧
by What_is_Solution @ 2024-12-07 17:25:14
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <cmath>
int main() {
int n;
std::cin >> n;
std::vector<double> nums(n);
for (int i = 0; i < n; ++i) {
std::cin >> nums[i];
}
std::sort(nums.begin(), nums.end());
double sum = 0;
for (int i = 1; i < n - 1; ++i) {
sum += nums[i];
}
double avg = sum / (n - 2);
double maxDiff = 0;
for (int i = 1; i < n - 1; ++i) {
double diff = std::fabs(nums[i] - avg);
maxDiff = std::max(maxDiff, diff);
}
std::cout << std::fixed << std::setprecision(2) << avg << " " << maxDiff << std::endl;
return 0;
}
by What_is_Solution @ 2024-12-07 17:25:47
(算改进的吧)。。。