1 条题解
-
0
P76047 去掉最高最低分 — 题解
专题: 函数
适用题目: 新生 OJ 本地题包中的 P76047,不要直接用于外站同名题。解题思路
对每个选手封装一个得分计算:总分减一个最高分和一个最低分,再取所有选手的最大结果。
复杂度
时间 ,额外空间 。
易错点
最高分和最低分相等也要分别减去两个评分位置。
C11 参考程序
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <limits.h> int get_score(const int *a,int m){ int sum=0,mi=INT_MAX,mx=INT_MIN; for(int i=0;i<m;i++){ sum+=a[i];if(a[i]<mi)mi=a[i];if(a[i]>mx)mx=a[i]; } return sum-mi-mx; } int main(void) { int n,m;scanf("%d%d",&n,&m);int *a=malloc(m*sizeof(int)),best=0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++)scanf("%d",&a[j]); int s=get_score(a,m);if(s>best)best=s; } printf("%d\n",best);free(a);return 0; }C++17 参考程序
#include <bits/stdc++.h> using namespace std; int get_score(const vector<int> &a) { int sum = 0, low = INT_MAX, high = INT_MIN; for (int x : a) { sum += x; low = min(low, x); high = max(high, x); } return sum - low - high; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; int best = 0; for (int i = 0; i < n; i++) { vector<int> a(m); for (int &x : a) cin >> x; best = max(best, get_score(a)); } cout << best << '\n'; return 0; }
题解建议设置为训练结束后开放。普通判题无法验证是否实际使用指定函数或 STL。
- 1
信息
- ID
- 1816
- 时间
- 2000ms
- 内存
- 256MiB
- 难度
- 2
- 标签
- 提交数
- 0
- 已通过
- 0
- 上传者