法1:
unordered_map(整合信息)+vector(排序)
难点
分块输入学生信息(用map整合)
整合信息后需要对信息处理、对学生排序(vector)
相关数据结构
用 map[HTML_REMOVED]存储姓名-学生,用于整合每个学生的信息
用 vector[HTML_REMOVED] 来对student进行排序,用于输出时按照成绩由高到低、同分按姓名由小到大的方式
注意:这里的student里也有姓名这一成员
Tricks
结构体初始化的方法:
构造函数初始化
结构体可以封装函数
以上解释搬运:Nbcs
#include<iostream>
#include<cstring>
#include<unordered_map>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
struct Student
{
string id;
int p,m,f,s;
Student()//初始化
{
p=m=f=-1;
s=0;
}
void calc()
{
if(f>=m) s = f;
else s = round(m*0.4+f*0.6);//四舍五入函数round()
}
bool operator< (const Student& t) const
{
if(s != t.s) return s>t.s;
return id<t.id;
}
};
/*
bool cmp(Student &x,Student &y)
{
if(x.s!=y.s) return x.s>y.s;
else return x.id<y.id;
}
*/
int main()
{
int p,m,n;
cin>>p>>m>>n;
unordered_map<string,Student> hash;
string id;
int s;
for(int i = 0; i<p; i++)
{
cin>>id>>s;
hash[id].id = id;
hash[id].p = s;
}
for(int i = 0; i<m; i++)
{
cin>>id>>s;
hash[id].id = id;
hash[id].m = s;
}
for(int i = 0; i<n; i++)
{
cin>>id>>s;
hash[id].id = id;
hash[id].f = s;
}
vector<Student> students;
for(auto item:hash)
{
auto stu = item.second;
stu.calc();//计算总成绩
if(stu.p >= 200 && stu.s >=60)
students.push_back(stu);
}
sort(students.begin(),students.end());
for(auto s:students)
cout<<s.id<<' '<<s.p<<' '<<s.m<<' '<<s.f<<' '<<s.s<<endl;
return 0;
}
法2:
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
using namespace std;
int n,m,k,score;
string id;
struct node{
int a,b,c,avg;
string id;
}temp;
map<string,node>mp;
vector<node> ans;
bool cmp(node a,node b)
{
if(a.avg == b.avg) return a.id<b.id;
return a.avg>b.avg;
}
int main()
{
cin>>n>>m>>k;
for(int i = 0; i<n; i++)
{
cin>>id>>score;
mp[id].a = score;
mp[id].b = -1;
mp[id].c = -1;
}
for(int i = 0; i<m; i++) cin>>id>>score,mp[id].b=score;
for(int i = 0; i<k; i++) cin>>id>>score,mp[id].c=score;
for(auto t = mp.begin(); t!=mp.end(); t++)
{
if(t->second.a<200) continue;
temp.id = t->first;
temp.a = t->second.a;
temp.b = t->second.b;
temp.c = t->second.c;
if(temp.c>=temp.b) temp.avg = temp.c;
else temp.avg = (temp.b*4+temp.c*6+5)/10;
if(temp.avg >= 60) ans.push_back(temp);
}
sort(ans.begin(),ans.end(),cmp);
for(int i = 0; i<ans.size(); i++)
{
cout<<ans[i].id;
printf(" %d %d %d %d\n",ans[i].a,ans[i].b,ans[i].c,ans[i].avg);
}
return 0;
}