跳转至

第七章 结构体

1147:最高分数的学生姓名

代码
#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;

    int maxn = -1;
    string ans;

    while (n--) {
        int x;
        string s;
        cin >> x >> s;

        if (x > maxn) {
            maxn = x;
            ans = s;
        }
    }

    cout << ans << endl;

    return 0;
}

1399:甲流病人初筛

代码
#include <bits/stdc++.h>

using namespace std;

int n;
int cnt;
string s;
double degree;
int flag;

int main(){
    cin >> n;
    while (n--){
        cin >> s >> degree >> flag;
        if (degree >= 37.5 && flag){
            cnt++;
            cout << s << '\n';
        }
    }
    cout << cnt << '\n';

    return 0;
}

1176:谁考了第 k 名

代码
#include <bits/stdc++.h>

using namespace std;

int n, k;

int main() {
    cin >> n >> k;

    vector<pair<double, int>> a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i].second >> a[i].first;

    sort(a.begin(), a.end());

    cout << a[n - k].second << ' ' << a[n - k].first << '\n';

    return 0;
}
代码02
#include <bits/stdc++.h>

using namespace std;

struct node {
    int id;
    double score;

    bool operator< (const node &W)const {
        return score > W.score;
    }
} a[110];

int main() {
    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
        cin >> a[i].id >> a[i].score;

    sort(a, a + n);

    printf("%d %g\n", a[k - 1].id, a[k - 1].score);

    return 0;
}
代码03
#include <bits/stdc++.h>

using namespace std;

pair<double, int> a[110];
int n, k;

int main() {
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        cin >> a[i].second >> a[i].first;

    sort(a, a + n, greater<pair<double, int> >());

    cout << a[k - 1].second << ' ' << a[k - 1].first << '\n';

    return 0;
}
代码04
#include <bits/stdc++.h>

using namespace std;

int n, k;

int main() {
    cin >> n >> k;

    priority_queue<pair<double, int> > q;
    for (int i = 0; i < n; i++) {
        int x;
        double y;
        cin >> x >> y;
        q.push({y, x});
    }

    k--;
    while (k--) {
        q.pop();
    }

    cout << q.top().second << ' ' << q.top().first<< '\n';

    return 0;
}
代码05
#include<bits/stdc++.h>

using namespace std;

int a[110];
double b[110];

int main() {
    int n,k;
    cin>>n>>k;

    for(int i=0; i<n; i++) {
        cin>>a[i]>>b[i];
    }

    for(int i=0; i<n; i++) { // 控制每趟
        for(int j=n-1; j>0; j--) { // 从后往前,把当前最小的,冒泡到左边
            if(b[j]<b[j-1]) {
                swap(b[j],b[j-1]);
                swap(a[j],a[j-1]);
            }
        }
    }

    printf("%d %g", a[n - k], b[n - k]);

    return 0;
}

1178:成绩排序

代码
#include <bits/stdc++.h>

using namespace std;

struct node {
    string name;
    int score;

    bool operator< (const node &W)const {
        if (score != W.score) return score > W.score;
        return name < W.name;
    }
} a[30];

int main() {
    int n;
    cin >> n;

    for (int i = 0; i < n; i++) cin >> a[i].name >> a[i].score;

    sort(a, a + n);

    for (int i = 0; i < n; i++) cout << a[i].name << ' ' << a[i].score << '\n';

    return 0;
}
代码02
#include<bits/stdc++.h>

using namespace std;

const int N = 30;

struct node {
    string name;
    int score;
    bool operator <(const node &p) const {
        if (score != p.score) return score > p.score;
        return name < p.name;
    }
} a[N];

int n;

void bubble_sort() {
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n - 1; j++)
            if (a[j + 1] < a[j])
                swap(a[j], a[j + 1]);
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i].name >> a[i].score;
    bubble_sort();
    for (int i = 0; i < n; i++) cout << a[i].name << ' ' << a[i].score << '\n';

    return 0;
}

1183:病人排队

代码
#include <bits/stdc++.h>

using namespace std;

const int N = 110;

struct node1 {
    int year;
    int id;
    string num;

    bool operator< (const node1 &W)const {
        if (year != W.year) return year > W.year;
        return id < W.id;
    }
} A[N];

string B[N];

int k1, k2;

int main() {
    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        string x;
        int y;
        cin >> x >> y;

        if (y >= 60) {
            A[k1].id = i, A[k1].year = y, A[k1].num = x;
            k1++;
        } else B[k2++] = x;
    }

    sort(A, A + k1);

    for (int i = 0; i < k1; i++) cout << A[i].num << '\n';
    for (int i = 0; i < k2; i++) cout << B[i] << '\n';

    return 0;
}

1839:【05NOIP 提高组】谁拿了最多奖学金

代码
#include <iostream>

using namespace std;

int n;
string top;
int maxn = -1;
int sum;

int main() {
    cin >> n;
    while (n--) {
        string s;
        int pingjun, pingyi;
        char ganbu, xibu;
        int lunwen;
        cin >> s >> pingjun >> pingyi >> ganbu >> xibu >> lunwen;

        int now = 0;
        if (pingjun > 80 && lunwen >= 1) now += 8000;
        if (pingjun > 85 && pingyi > 80) now += 4000;
        if (pingjun > 90) now += 2000;
        if (pingjun > 85 && xibu == 'Y') now += 1000;
        if (pingyi > 80 && ganbu == 'Y') now += 850;

        if (now > maxn) {
            maxn = now;
            top = s;
        }

        sum += now;
    }

    cout << top << '\n' << maxn << '\n' << sum << '\n';

    return 0;
}
代码02
#include <bits/stdc++.h>

using namespace std;

const int N = 110;

struct node {
    string name;
    int a, b;
    char c, d;
    int e;
    int mon;

    node(string _name = "", int _a = 0, int _b = 0, char _c = 'Y', char _d = 'Y', int _e = 0): name(_name), a(_a), b(_b), c(_c), d(_d), e(_e) {}
} A[N];

int n, sum, ansmon = -1;
string ansname;

int check(int a, int b, char c, char d, int e) {
    int t = 0;
    if (a > 80 && e>= 1) t += 8000;
    if (a > 85 && b > 80) t += 4000;
    if (a > 90) t += 2000;
    if (d == 'Y' && a > 85) t += 1000;
    if (c == 'Y' && b > 80) t += 850;

    return t;
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        string name;
        int a, b, e;
        char c, d;
        cin >> name >> a >> b >> c >> d >> e;

        A[i] = node(name, a, b, c, d, e);
        A[i].mon = check(a, b, c, d, e);

        sum += A[i].mon;
        if (A[i].mon > ansmon) {
            ansmon = A[i].mon;
            ansname = A[i].name;
        }
    }

    cout << ansname << '\n' << ansmon << '\n' << sum << '\n';

    return 0;
}