Z Courses

A Simple Web Classroom

用户工具

站点工具


趣题:cpp:期末考试排名

期末考试排名

题目描述

期末考试结束了,班主任想对本班学生的成绩进行一个排名。大概的思路是确定要排名的人数,然后输入这些同学的姓名、语文、数学、外语成绩,最后输出学生姓名、总分及班级排名。

代码参考

#include <iostream>
using namespace std;
struct st{
    string name;
    float chinese;
    float math;
    float english;
    float sum;
    int rank; 
};
int main()
{
    int n;
    cout <<"请输入学生数:";
    cin >> n;
    st st1[n];
    cout << "请按照以下提示输入:"<<endl;
    cout <<"姓名    语文    数学    外语"<<endl;
    for(int i=0;i<n;i++)
    {
	cin >> st1[i].name >> st1[i].chinese>>st1[i].math>>st1[i].english;
	st1[i].sum=st1[i].chinese+st1[i].math+st1[i].english;
    }
	
    for(int i=0;i<n;i++)
    {
	int c=1;
	for(int j=0;j<n;j++)
	{
	    if(st1[i].sum<st1[j].sum)
		c++;
	}
	st1[i].rank=c;
    }
	
    for(int i=0;i<n;i++)
    {
	cout << st1[i].name <<"的总分是:"<<st1[i].sum<<",班级排名:"<<st1[i].rank<<endl;
    }
}