第一个点不对,大佬帮看看

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

@[qew12312](/user/916027) ```cpp #include<iostream> #include<cstring> using namespace std; struct a{ char qw[1000]; int b[3]; }; int main() { double sum,sum1=0;int n,t,we,as; cin>>n; a er[1005]; for(t=0;t<n;t++){ sum=0; cin>>er[t].qw;//为什么这里用cin.getline就只能输入一次? for(we=0;we<3;we++){ cin>>er[t].b[we]; sum+=er[t].b[we];//输入 } if(sum>sum1) { sum1=sum;as=t; }//判断总分大小 } cout<<er[as].qw<<" "; for(we=0;we<3;we++){ cout<<er[as].b[we]<<" "; }//输出 return 0; } ```
by guozhetao @ 2023-01-13 18:01:09


@[qew12312](/user/916027) 遇到 RE 就开大一点
by guozhetao @ 2023-01-13 18:01:48


楼上正解
by Cindy_Li @ 2023-01-13 18:05:53


@[qew12312](/user/916027) >为什么这里用cin.getline就只能输入一次? 因为`getline`是忽略空格读入整行作为一个字符串,而`cin>>er[t].qw`默认以空格作为分界点 在这道题里会把第一行( $n$ 后面的多余空格和换行)作为一个字符串读入。 在执行读入分数的时候会读入第二行的名字,而这是不符合`int`格式的,所以会直接终止读入。 输出的 $3$ 个数因为定义在`main`函数里,是初始随机给的
by Cindy_Li @ 2023-01-13 18:19:39


@[qew12312](/user/916027) 一点建议 1. 变量定义到全局(尤其数组),这时所有变量初始值为`0` 1. 慎用`double`,运算中只会出现整数就不用 1. 把数组空间开够,一般会多开 $5$ ~ $50$ 例子: ```c++ #include<iostream> #include<cstring> using namespace std; struct a{ string qw; int b[5]; }er[1005]; int sum,sum1; int n,t,we,as; int main(){ return 0; } ```
by Cindy_Li @ 2023-01-13 18:30:33


@[xinyulicindy0608](/user/565091) 改了,对了,但想不清结构体数组为什么要开大一点,它不是默认是int类型吗
by qew12312 @ 2023-01-13 20:32:02


@[qew12312](/user/916027) 怎么说呢,按理说你的代码不应该RE,我本地测第一个测试点也没问题 数组开大一点是习惯,反正大一点点对空间影响可以忽略,谨慎一点避免出错(尤其像我喜欢下标从1开始 PS:突然想起教练说过函数内开数组空间限制特别小,可能是这个问题 以后尽量数组开**全局**,不要在主函数里定义
by Cindy_Li @ 2023-01-13 21:06:46


|