纠错#include<bits/stdc++.h> using namespace std; struct stu{ int name; double score[10]; double ave; }; bool cmp(stu a,stu b){ return a.ave>b.ave; }; int main(){ int T,m,n; cin>>T; while(T--){ cin>>n>>m; stu a[n]; for(int i=0;i<n;i++){ a[i].name=i+1; a[i].ave=0; for(int j=0;j<m;j++){ cin>>a[i].score[j]; a[i].ave=a[i].ave+a[i].score[j]; } a[i].ave=a[i].ave/m; } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(a[i].score[j]<60){ a[i].ave=0;break; } } } sort(a,a+n,cmp); if(a[0].ave==0)cout<<"NULL"; for(int i=0;i<n;i++){ if(i&&a[i].ave)cout<<' '; if(a[i].ave)cout<<a[i].name; } if(T)cout<<endl; } }
时间: 2024-01-17 11:06:09 浏览: 33
这段代码实现了一个成绩排名的功能,但是有一些问题需要纠正。
1. 头文件应该是 `#include <iostream>`,而不是 `#include<bits/stdc++.h>`
2. 结构体中的 `name` 应该是字符串类型,而不是整型,因为学生姓名一般不是用数字表示的。
3. 在计算学生平均分时,应该使用 `double` 类型进行计算,以保留小数位数。
4. 对于每个学生,如果有科目不及格,则该学生的平均分应该为 0,而不是把该科目的成绩赋值为 0。
5. 在输出排名时,如果第一名的平均分为 0,应该输出 "NULL",而不是什么都不输出。
经过修改后的代码如下所示:
```c++
#include <iostream>
#include <algorithm>
using namespace std;
struct stu {
string name;
double score[10];
double ave;
};
bool cmp(stu a, stu b) {
return a.ave > b.ave;
}
int main() {
int T, m, n;
cin >> T;
while (T--) {
cin >> n >> m;
stu a[n];
for (int i = 0; i < n; i++) {
cin >> a[i].name;
a[i].ave = 0;
for (int j = 0; j < m; j++) {
cin >> a[i].score[j];
a[i].ave += a[i].score[j];
}
a[i].ave /= m;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i].score[j] < 60) {
a[i].ave = 0;
break;
}
}
}
sort(a, a + n, cmp);
if (a[0].ave == 0) cout << "NULL";
for (int i = 0; i < n; i++) {
if (i && a[i].ave) cout << ' ';
if (a[i].ave) cout << a[i].name;
}
if (T) cout << endl;
}
return 0;
}
```
相关推荐
![h](https://img-home.csdnimg.cn/images/20210720083646.png)
![h](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)