Future_Comet @ 2024-08-22 13:48:28
代码如下,样例已过,自己造的数据也过了,30分,求大佬解惑。
#include<bits/stdc++.h>
using namespace std;
int n;
float sum,a[310],ans;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
for(int i=2;i<n;i++)
sum+=a[i];
sum/=n-2;
ans=max(sum-a[1],a[n]-sum);
printf("%.2lf %.2lf",sum,ans);
return 0;
}
by giao_nice @ 2024-08-22 13:51:07
@Future_Comet sum/=n-2没加括号,应为sum/=(n-2)
by giao_nice @ 2024-08-22 13:53:56
还有,这个代码并没有过样例,在题目中说是有效样本去求ans,所以应该是ans=max(sum-a[2],a[n-1]-sum);
by wanchenhao @ 2024-08-22 13:54:47
@Future_Comet
AC代码求关
#include <bits/stdc++.h>
using namespace std;
double a[310];
double cnt, m, w, p;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i < n; i++) {
for (int j = 1; j < n; j++) {
if (a[j] >= a[j+1]) {
swap(a[j], a[j + 1]);
}
}
}
m = a[n-1];
for (int i = 2; i < n; i++) {
cnt+=a[i];
}
p=cnt/(n-2);
double w=-1,q;
for(int i=2;i<n;i++){
q=fabs(a[i]-p);
if(q>=w){
w=q;
}
}
printf("%.2f %.2f",p,w);
return 0;
}
by wanchenhao @ 2024-08-22 13:55:36
@Future_Comet
用你的代码调的
by Future_Comet @ 2024-08-22 14:00:33
牛逼,感谢大佬
by Future_Comet @ 2024-08-22 14:01:50
@giao_nice 大佬,刚才我自己提交了一下,括号不加好像也行
by giao_nice @ 2024-08-22 14:12:53
@Future_Comet 你对比一下10/5-2he10/(5-2)的区别,你就知道括号的重要性
by Future_Comet @ 2024-08-22 18:41:34
@giao_nice 对不起下午有事出去了没看回复
我记得好像sum/=n-2是先运算n-2再运算sum/=...的吧?
如大佬还在,求解惑。
by Future_Comet @ 2024-08-22 18:45:33
@giao_nice 实测:
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int ans=10;
ans/=5-2;
cout<<ans;
return 0;
}
输出3
by giao_nice @ 2024-08-22 21:14:40
@Future_Comet 哦,我记错了,是ans=ans/5-2才会出问题。