POJ-3325-ICPC Score Totalizer Software-解题报告

题目比较简单。

思路:读入一组分数,去除最高和最低分,再求余下分数的平均分即可。

实现:用STL的vector很方便,接着用到sort算法,自己也不用写快排了。

收获:发现将变量的声明放在循环体while内,时间会不所增加;而放在外面效率则高些。

代码一:

/*Memory: 716K Time: 0MS */ #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> scores; int score; double sumScore; int aveScore; int n; while(cin >> n, n !=0) { scores.clear(); for(int i = 0; i < n; i++) { cin >> score; scores.push_back(score); } sort(scores.begin(), scores.end()); scores.erase(scores.end() - 1, scores.end()); scores.erase(scores.begin(), scores.begin() + 1); sumScore = 0.0; for(vector<int>::iterator iter = scores.begin(); iter != scores.end(); iter++) { sumScore += *iter; } aveScore = (int)(sumScore / scores.size()); cout << aveScore << endl; } return 0; }

代码二:

/*Memory: 716K Time: 32MS */ #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { //vector<int> scores; //int score; //double sumScore; //int aveScore; int n; while(cin >> n, n !=0) { //scores.clear(); vector<int> scores; int score; for(int i = 0; i < n; i++) { cin >> score; scores.push_back(score); } sort(scores.begin(), scores.end()); scores.erase(scores.end() - 1, scores.end()); scores.erase(scores.begin(), scores.begin() + 1); //sumScore = 0.0; double sumScore = 0.0; for(vector<int>::iterator iter = scores.begin(); iter != scores.end(); iter++) { sumScore += *iter; } int aveScore = (int)(sumScore / scores.size()); cout << aveScore << endl; } return 0; }

你可能感兴趣的:(算法,vector,iterator)