其他分享
首页 > 其他分享> > 奖学金(归并排序)

奖学金(归并排序)

作者:互联网

 1 #include <iostream>
 2 using namespace std;
 3 
 4 struct score{
 5     int num;
 6     int chs;
 7     int sum;
 8 } sco[300], r[300];
 9 
10 void value(score*, score*);
11 void msort(int, int);
12 
13 int main() {
14     int n, c, m, e;
15     cin >> n;
16     for (int i=0; i<n; i++) {
17         cin >> c >> m >> e;
18         sco[i].chs = c;
19         sco[i].sum = c+m+e;
20         sco[i].num = i;
21     }
22     msort(0, n-1);
23     for (int i=0; i<5; i++)
24         cout << sco[i].num+1 << " " << sco[i].sum << endl;
25     return 0;
26 }
27 
28 void value(score* x, score* y) {
29     x->sum = y->sum;
30     x->num = y->num;
31     x->chs = y->chs;
32 }
33 
34 void msort(int s, int t) {
35     if (s==t) return;
36     int mid=(s+t)/2;
37     msort(s, mid);
38     msort(mid+1, t);
39     int i=s, j=mid+1, k=s;
40     while (i<=mid && j<=t)
41         if (sco[i].sum>sco[j].sum) {
42             value(&r[k], &sco[i]); k++; i++;
43         } else if (sco[i].sum<sco[j].sum) {
44             value(&r[k], &sco[j]); k++; j++;
45         } else {
46             if (sco[i].chs>=sco[j].chs) {
47                 value(&r[k], &sco[i]); k++; i++;
48             } else {
49                 value(&r[k], &sco[j]); k++; j++;
50             }
51         }
52     while (i<=mid) {
53         value(&r[k], &sco[i]); k++; i++;
54     }
55     while (j<=t) {
56         value(&r[k], &sco[j]); k++; j++;
57     }
58     for (int i=s; i<=t; i++) value(&sco[i], &r[i]);
59 }

 

标签:归并,int,sum,sco,msort,++,chs,排序,奖学金
来源: https://www.cnblogs.com/vergica/p/15704567.html