題目連結 http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=b021。
此題用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;
- }
沒有留言:
張貼留言