LZJ135 @ 2024-06-22 12:10:47
为什么???
#include<iostream>
using namespace std;
struct stu
{
string n;
int x=0;
int c,m,e;
long long z;
};
int main()
{
int p,o=0;
cin>>p;
struct a[p];
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].x=a[i].n+a[i].c+a[i].m+a[i].e;
}
for(int i=1;i<p;i++)
{
if(a[i].x>a[i-1].x)
{
o=i;
}
}
cout<<a[o].n<<a[o].c<<a[o].m<<a[o].e;
return 0;
}
by LZJ135 @ 2024-06-22 12:12:14
输出了:expected primary-expression before '[' token
为什么???
by Eva_91418 @ 2024-06-22 12:16:02
a[i].x++;
这里++总分的目的是(?)
if(a[i].x>a[i-1].x)
{
o=i;
}
以及这个打擂台有问题(
可以试试自定义排序(
by Eva_91418 @ 2024-06-22 12:17:38
这里输出时记得加空格(
cout<<a[o].n<<a[o].c<<a[o].m<<a[o].e;
by yx666 @ 2024-06-22 12:18:16
@LZJ135
struct a[p];
C++ 不支持可变数组
没有变量类型
猜你想写
stu a[1000];
by yx666 @ 2024-06-22 12:22:14
@LZJ135
#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 wangjingxi_ @ 2024-06-22 13:45:52
不能这样定义动态数组,附上AC代码:
#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:46:47
@LZJ135