There are some students’ grade records, in format of “name grade”. In order to make the teacher to view and manage all the records easily, you are asked to develop a program to sort the records first by grade (in descending order), and second by name (in alphabetic order). That means the sorted records will be from the highest grade to lowest grade; and if two students have the same grade, whose name appeared earlier in a dictionary will be shown earlier.
输入
8 grade records, one record on per line.
Only letters and digits appear in the name of a student (i.e. there will be no space in a name). All grades are integers.
输出
The 8 sorted records, one record per line.
样例输入
WangYi 90
LiMing 90
HuRui 50
ZhangLi 65
LiLei 80
ZhangFei 65
ZhaoSan 70
ChenZi 100
样例输出
ChenZi 100
LiMing 90
WangYi 90
LiLei 80
ZhaoSan 70
ZhangFei 65
ZhangLi 65
HuRui 50
#include
#include
#include
#include
//#include "finished.h"
#include
#include
using namespace std;
struct Student
{
char name[30];
int grade;
};
Student Stu[30];
int main()
{
int i,j;
for (i = 0; i cin >> Stu[i].name >> Stu[i].grade;
for (i = 0; i < 7; i++)
{
for (j = i + 1; j <8; j++)
{
if (Stu[i].grade < Stu[j].grade)
{
char temp1[30];
strcpy(temp1, Stu[i].name);
strcpy(Stu[i].name, Stu[j].name);
strcpy(Stu[j].name, temp1);
int temp;
temp = Stu[i].grade;
Stu[i].grade = Stu[j].grade;
Stu[j].grade = temp;
}
}
}
for (i = 0; i < 7; i++)
{
for (j = i + 1; j <8; j++)
{
if ((Stu[i].grade == Stu[j].grade) && strcmp(Stu[i].name, Stu[j].name) == 1)
{
char temp1[30];
strcpy(temp1, Stu[i].name);
strcpy(Stu[i].name, Stu[j].name);
strcpy(Stu[j].name, temp1);
int temp;
temp = Stu[i].grade;
Stu[i].grade = Stu[j].grade;
Stu[j].grade = temp;
}
}
}
for (i = 0; i < 8; i++)
cout<< Stu[i].name<<" "<< Stu[i].grade<<endl;
//char str[300];
// cin.getline(str, 300);
//strlwr(str);
/* cout << char(tolower('A')) << endl;*/
/*cout << right << fixed;
cout.precision(2);
cout<<"("<<x[n]<<" "<<y[n]<<" "<<z[n]<<") "<<min_d<<endl;*/
return 0;
}
for (i = 0; i cin >> Stu[i].name >> Stu[i].grade;
这句没法通过编译吧!
for(i=0;i cin >> Stu[i].name >> Stu[i].grade;
}
懂了,这上面有些字符输入不会显示