台中女中程式解題系統:b021: 指考分發
題目連結 http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=b021。
此題用struct並改寫C++語言sort的比較函數comp即可。
此題用struct並改寫C++語言sort的比較函數comp即可。
- struct Score
- {
- int n;
- int total;
- int math;
- };
- int cmp(Score s1, Score s2)
- {
- if(s1.total == s2.total)
- {
- return s1.math > s2.math;
- }
- else
- return s1.total > s2.total;
- }
程式碼:
- #include <iostream>
- #include <vector>
- #include <algorithm>
- struct Score
- {
- int n;
- int total;
- int math;
- };
- int cmp(Score s1, Score s2)
- {
- if(s1.total == s2.total)
- {
- return s1.math > s2.math;
- }
- else
- return s1.total > s2.total;
- }
- using namespace std;
- int main()
- {
- int n;
- cin >> n;
- Score s[n];
- for(int i = 0; i < n; i++)
- {
- int no, chi, eng, math, phy, che;
- cin >> no >> chi >> eng >> math >> phy >> che;
- s[i].n = no;
- s[i].total = chi + eng + math + phy + che;
- s[i].math = math;
- }
- sort(s, s + n, cmp);
- for(int i = 0; i < n; i++)
- cout << s[i].n << endl;
- return 0;
- }
若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。
If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.
留言