1. 程式人生 > >1077 互評成績計算

1077 互評成績計算

位置 stream i++ vector 註意 ref space int set

題目鏈接:https://pintia.cn/problem-sets/994805260223102976/problems/994805262303477760

題解:

 1 #include <iostream>
 2 #include<vector>
 3 using namespace std;
 4 
 5 int main() {
 6     int n, m;
 7     cin >> n >> m;
 8     for (int i = 0; i < n; i++) {
 9         double G2;//定義為double便於後面計算final_score
10 cin >> G2; 11 vector<int>v; 12 for (int i = 0; i < n - 1; i++) { 13 int temp; 14 cin >> temp; 15 v.push_back(temp); 16 } 17 for (int i = 0; i < v.size(); i++) { 18 if (v[i]<0 || v[i]>m) {
19 v.erase(v.begin() + i); 20 i--;//保證刪除後叠代器可以指向刪除前的位置 21 } 22 } 23 int max = *v.begin(), min = *v.begin(), sum = 0; 24 for (int i = 0; i < v.size(); i++) { 25 if (max < v[i]) max = v[i]; 26 if (min > v[i]) min = v[i];
27 sum += v[i]; 28 } 29 double G1 = (double)(sum - max - min) / (v.size() - 2);//註意求平均時要減去最高分,最低分的兩個組 30 int final_score = (G1 + G2) / 2 + 0.5;//加0.5保證四舍五入 31 cout << final_score << endl; 32 } 33 return 0; 34 }

1077 互評成績計算