A1036
题目大意:给出N个同学的信息,输出女生中的最高分获得者的信息与男生中最低分获得者的信息,并输出他们的分数差。如果不存在女生或者男生,则对应获得者信息处输出Absent,而且差值处输出NA
以下是我写的代码:
#include<iostream>
using namespace std;
int main()
{
string maxname="0",minname="0",name,gender,id,minid,maxid,maname,miname;
int n,grade,mingrade=100,maxgrade=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>name>>gender>>id>>grade;
if(grade>=0&&grade<=100)
{
if(gender=="M")
{
if(grade<=mingrade)
{
mingrade=grade;
minname=name;
minid=id;
}
}
if(gender=="F")
{
if(grade>=maxgrade)
{
maxgrade=grade;
maxname=name;
maxid=id;
}
}
}
}
if(maxname=="0")
{
maxname="Absent";
maxid=" ";
}
if(minname=="0")
{
minname="Absent";
maxid=" ";
}
if(maxname=="Absent" or minname=="Absent")
{
cout<<maxname<<" "<<maxid<<endl<<minname<<" "<<minid<<endl<<"NA";
}
if(maxname!="Absent" and minname!="Absent")
{
cout<<maxname<<" "<<maxid<<endl<<minname<<" "<<minid<<endl<<maxgrade-mingrade;
}
return 0;
}
太麻烦了,我都没通过
照着柳神的写一遍
改成了这样的
#include<iostream>
using namespace std;
int main()
{
string female,male;
int n,mingrade=101,maxgrade=-1;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
string gender,name,id;
int grade;
cin>>name>>gender>>id;
scanf("%d",&grade);
if(gender=="M"&&grade<mingrade)
{
mingrade=grade;
male=name+" "+id;
}
if(gender=="F"&&grade>maxgrade)
{
maxgrade=grade;
female=name+" "+id;
}
}
if(maxgrade!=-1)
{
cout<<female<<endl;
}else printf("Absent\n");
if(mingrade!=101)
{
cout<<male<<endl;
}else printf("Absent\n");
if(maxgrade!=-1&&mingrade!=101)
{
printf("%d",maxgrade-mingrade);
}else
printf("NA");
return 0;
}
因为有可能定义多个string类会超时,所以把能写在for循环里定义的string类写在for循环里,尽量用scanf和printf,如果嫌一个一个的输出太过麻烦,还要定义好几个字符,那就直接像这样male=name+" "+id;直接输出male就好了,也不用多写然后担心超时。日常爱柳神小姐姐!