C++新手:输入流错误 求大佬康康

P5740 【深基7.例9】最厉害的学生

像素旋转 @ 2021-01-31 14:04:13

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int N = 1000;
class Student {
public:
    char name[9];
    int chinese;
    int math;
    int english;

    Student() {
        name[0] = '0';
        chinese = 0;
        math = 0;
        english = 0;
        sum = 0;
    };

    int summary() {
        return sum=chinese + math + english;
    }

private:
    int sum;
};
int main(void)
{
    int n;
    cin >> n;
    cin.get();
    Student** stu;
    stu = new Student*[n];
    int* sum = new int[n];
    int max = 0;
    int ans = 0;
    for (auto i = 0; i < n; i++){
        cin >>stu[i]->name>>stu[i]->chinese >> stu[i]->math >> stu[i]->english;
        sum[i]=stu[i]->summary();
        if (max < sum[i]) {
            max = sum[i];
            ans = i;
        }
    }
    cout << stu[ans]->name << " " << stu[ans]->chinese
        << " " << stu[ans]->math << " " << stu[ans]->english << endl;

    return 0;
}

by JohnFKennedy @ 2021-02-02 18:14:17

@像素旋转 别的题估计能过


by Deep_Breath @ 2021-04-25 20:37:45

可以直接用结构体啊? AC code:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
int const N = 100000 + 5;
/*数组开外面*/
struct s {
    string xm;
    int yw, sx, yy;
};
int main () {
    int i, n;
    s mx, p;
    mx.yw = 0;
    mx.sx = 0;
    mx.yy = 0;
    cin >> n;
    while (n--) {
        cin >> p.xm >> p.yw >> p.sx >> p.yy;
        if (p.yw + p.sx + p.yy > mx.yw + mx.sx + mx.yy)  {
            mx = p;
        }
    }
    cout << mx.xm << " " << mx.yw << " " << mx.sx << " " << mx.yy;
    return 0;
}

上一页 |