日期:2025年11月2日
ZeroJudge 題目連結:g307. pA. 為了好吃的蘋果派(Apple Pie)
解題想法
依序讀取編號 i = 0 到 n-1 的分數,將分數排序後存入串列 scores,計算除了最高、最低分數以外的平均分數 avg,如果 avg 大於等於 t,將 i 存入答案之中。
Python 程式碼
使用時間約為 0.5 s,記憶體約為 3.7 MB,通過測試。
n, k, t = map(int, input().split())
ans = []
for i in range(n):
scores = sorted(map(int, input().split()))
avg = sum(scores[1:-1])/(k-2)
if avg >= t: ans.append(i)
if not ans:
print("A is for apple.")
else:
for a in ans:
print(a)
C++ 程式碼
使用時間約為 0.1 s,記憶體約為 284 kB,通過測試。
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n, k, t; scanf("%d %d %d", &n, &k, &t);
vector<int> ans;
for(int i=0; i<n; i++) {
vector<int> scores (k);
for(int j=0; j<k; j++) scanf("%d", &scores[j]);
sort(scores.begin(), scores.end());
int isum = 0;
for(int j=1; j<k-1; j++) isum += scores[j];
float avg = 1.0*isum/(k-2);
if (avg >= t) ans.push_back(i);
}
if (ans.empty()) {
printf("A is for apple.\n");
} else {
for(int a : ans) printf("%d\n", a);
}
return 0;
}
沒有留言:
張貼留言