求助

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

输出了:expected primary-expression before '[' token ------------ 为什么???
by LZJ135 @ 2024-06-22 12:12:14


```cpp a[i].x++; ``` 这里++总分的目的是(?) ```cpp if(a[i].x>a[i-1].x) { o=i; } ``` 以及这个打擂台有问题( 可以试试自定义排序(
by Eva_91418 @ 2024-06-22 12:16:02


这里输出时记得加空格( ```cpp cout<<a[o].n<<a[o].c<<a[o].m<<a[o].e; ```
by Eva_91418 @ 2024-06-22 12:17:38


@[LZJ135](/user/1293970) ```cpp struct a[p]; ``` 1. C++ 不支持可变数组 2. 没有变量类型 猜你想写 ``` cpp stu a[1000]; ```
by yx666 @ 2024-06-22 12:18:16


@[LZJ135](/user/1293970) ``` cpp #include<iostream> using namespace std; struct stu { string n; int x=0; int c,m,e; }; int main() { int p,o=0; cin>>p; stu a[1000]; for(int i=0;i<p;i++){ cin>>a[i].n>>a[i].c>>a[i].m>>a[i].e; a[i].x=a[i].c+a[i].m+a[i].e; } for(int i=1;i<p;i++) { if(a[i].x>a[o].x) { o=i; } } cout<<a[o].n<<' ' <<a[o].c<<' ' <<a[o].m<<' '<<a[o].e; return 0; } ``` 把你的改了一下。
by yx666 @ 2024-06-22 12:22:14


不能这样定义动态数组,附上AC代码: ```#include<iostream> #include<vector> #include<string> using namespace std; struct Student { string name; int total = 0; int chinese, math, english; }; int main() { int p, max_index = 0; cin >> p; vector<Student> students(p); for(int i = 0; i < p; i++) { cin >> students[i].name >> students[i].chinese >> students[i].math >> students[i].english; students[i].total = students[i].chinese + students[i].math + students[i].english; } for(int i = 1; i < p; i++) { if(students[i].total > students[max_index].total) { max_index = i; } } cout << students[max_index].name << " " << students[max_index].chinese << " " << students[max_index].math << " " << students[max_index].english << endl; return 0; } ``` 本人实测AC,求关。
by wangjingxi_ @ 2024-06-22 13:45:52


@[LZJ135](/user/1293970)
by wangjingxi_ @ 2024-06-22 13:46:47


|