A1137
个人思路
本题属于模拟题,按题目要求使用结构体排序即可
注意数组大小不能只开到10010,可能P,M,N中包含的用户各不相同,应该开到30000,否则最后一个测试点会出现段错误
英文理解
The final grade is calculated by G=(Gmid−term × 40% + Gfinal × 60%) if Gmid−term > Gfinal, or Gfinal will be taken as the final grade G.
if条件句后置,当Gm > Gf,按4:6计算
个人思路代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 30010;
int P, M, N;
struct Student{
string name;
double Gp = -1;
double Gm = -1;
double Gf = -1;
double G = -1;//总分G
}stu[maxn];
unordered_map<string, int> mp;//名字与下标的映射
bool cmp(Student s1, Student s2)
{
if(s1.G != s2.G)
return s1.G > s2.G;
else
return s1.name < s2.name;
}
int main(int argc, char *argv[]) {
scanf("%d%d%d", &P, &M, &N);
for(int i = 0; i < P; ++i)
{
cin >> stu[i].name >> stu[i].Gp;
mp[stu[i].name] = i;
}
//存在新输入的用户
int pos = P;
for(int i = 0; i < M; ++i)
{
string nn;
int ss;
cin >> nn >> ss;
if(mp.count(nn) == 0)//新用户
{
stu[pos].name = nn;
stu[pos].Gm = ss;
mp[nn] = pos++;
}
else
{
int id = mp[nn];
stu[id].Gm = ss;
}
}
for(int i = 0; i < N; ++i)
{
string nn;
int ss;
cin >> nn >> ss;
if(mp.count(nn) == 0)//新用户
{
stu[pos].name = nn;
stu[pos].Gf = ss;
mp[nn] = pos++;
}
else
{
int id = mp[nn];
stu[id].Gf = ss;
}
}
//cout << endl;
for(int i = 0; i < pos; ++i)
{
if(stu[i].Gm > stu[i].Gf)
stu[i].G = 0.4 * stu[i].Gm + 0.6 * stu[i].Gf;
else
stu[i].G = stu[i].Gf;
stu[i].G = round(stu[i].G);
//cout << stu[i].name << " " << stu[i].G << endl;
}
sort(stu, stu + pos, cmp);
for(int i = 0; i < pos; ++i)
{
if(stu[i].Gp >= 200 && stu[i].G >= 60)
cout << stu[i].name << " " << stu[i].Gp << " " << stu[i].Gm << " " << stu[i].Gf << " " << stu[i].G << endl;
}
return 0;
}