日期:2025年3月9日
ZeroJudge 題目連結:f441. 評分系統 Score
解題想法
因為 Python 一次讀取一行資料,所以在 Python 程式碼中我是用 zip 將正確答案及學生的答案綁在一起讀取。在 C++ 中則是一次讀取一個整數,遇到空格就停下來,可以每次讀一個學生的答案計分。
Python 程式碼
使用時間約為 20 ms,記憶體約為 3.3 MB,通過測試。
import sys
for line in sys.stdin:
n, s = map(int, line.split()) # 題數 n,每題分數 s
ans = list(map(int, input().split())) # 答案
m = int(input()) # 待批改試卷數量
for _ in range(m): # 執行 m 次
cnt = 0 # 答對題數
stu = list(map(int, input().split())) # 學生的答案
for a, b in zip(ans, stu): # 由 ans 及 stu 依序讀取資料
if a == b: cnt += 1 # 如果 a 等於 b,cnt 加 1
print(cnt*s) # 印出分數
C++ 程式碼
使用時間約為 2 ms,記憶體約為 352 kB,通過測試。
#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, s; // 題數 n,每題分數 s
while(cin >> n >> s) {
int ans[n]; // 答案
for(int i=0; i<n; i++) cin >> ans[i];
int m; cin >> m; // 待批改試卷數量
for(int i=0; i<m; i++) { // 執行 m 次
int cnt = 0; // 答對題數
for(int j=0; j<n; j++) { // 依序讀取 ans 及學生的答案
int b; cin >> b;
if (ans[j] == b) cnt++; // 如果 ans[j] 等於 b,cnt 加 1
}
cout << cnt*s << "\n"; // 印出分數
}
}
return 0;
}
沒有留言:
張貼留言