RockyQ012 @ 2024-07-11 20:08:38
我居然连普及— 都过不了QwQ
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
int a[n];
cin >> n;
int ans = 0;
for(int i = 1 ; i <= n ; ++i) cin >> a[i];
sort(a + 1 , a + n + 1);
for(int i = 2 ; i <= n - 1 ; ++i) {
ans += a[i];
}
double sum = ans / (n - 2);
cout << fixed << setprecision(2) << sum;
return 0;
}
没有输出?
by 残阳如血 @ 2024-07-11 20:09:31
@RockyQ012 你这数组怎么开的
by RockyQ012 @ 2024-07-11 20:11:21
不可以这么开吗?
by qiuribomu @ 2024-07-11 20:12:05
@RockyQ012 那也需要在输入数据后
by RockyQ012 @ 2024-07-11 20:14:31
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
int ans = 0;
int a[n];
for(int i = 1 ; i <= n ; ++i) cin >> a[i];
sort(a + 1 , a + n + 1);
for(int i = 2 ; i <= n - 1 ; ++i) {
ans += a[i];
}
double sum = ans / (n - 2);
cout << fixed << setprecision(2) << sum;
return 0;
}
还是不对
by WEICY123 @ 2024-07-11 20:16:13
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
int a[1000001];
cin >> n;
double ans = 0;
for(int i = 1 ; i <= n ; ++i) cin >> a[i];
sort(a + 1 , a + n + 1);
for(int i = 2 ; i <= n - 1 ; ++i) {
ans += a[i];
}
double sum = ans / (n - 2);
cout << fixed << setprecision(2) << sum;
return 0;
}
@RockyQ012 n在此题中是变量。数组const int n才能用a[n],ans要用double类型的
by dino @ 2024-07-11 20:16:48
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
int a[n + 1];
int ans = 0;
for(int i = 1 ; i <= n ; ++i) cin >> a[i];
sort(a + 1 , a + n + 1);
for(int i = 2 ; i <= n - 1 ; ++i) {
ans += a[i];
}
double sum = ans / ((n - 2) * 1.0);
cout << fixed << setprecision(2) << sum;
return 0;
}
@RockyQ012
by STA_Morlin @ 2024-07-11 20:17:32
@qiuribomu 不可以。
by sshhgggddsbf @ 2024-07-11 20:17:45
int a[n]放cin后面,且ans开double
by dino @ 2024-07-11 20:19:01
@RockyQ012 a[n]下标0~n-1,需要加个1。然后除法和/前后变量类型有关,你这里都为int,不能进行小数运算,要*1.0
by qiuribomu @ 2024-07-11 20:19:23
@STA_Morlin 可以啊,输入后就有数据了,可以使用输入的值去开数组