#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;
struct node{
string id;
int pScore=-1, mScore=-1, fScore=-1;
int g=-1;
};
bool cmp(node a, node b){
if(a.g != b.g) return a.g > b.g;
else return a.id < b.id;
}
map<string,node> stuMap;
vector<node> stuVec;
int main(){
int p,m,n;
string s;
int score;
scanf("%d%d%d",&p,&m,&n);
for(int i = 0; i < p;i++){
cin>> s;
scanf("%d",&score);
if(score >= 200){
stuMap[s].id =s;
stuMap[s].pScore =score;
}
}
for(int i = 0; i < m;i++){
cin>> s;
scanf("%d",&score);
if(stuMap.count(s)){//即符合编程大于等于200分
stuMap[s].mScore = score;
}
}
int num = 0;
for(int i = 0; i<n;i++){
cin>> s;
scanf("%d",&score);
if(stuMap.count(s)){//即符合编程大于等于200分
stuMap[s].fScore = score;
if(stuMap[s].mScore > stuMap[s].fScore){
stuMap[s].g = (int)(stuMap[s].mScore * 0.4 + stuMap[s].fScore * 0.6 + 0.5);
}else{
stuMap[s].g = stuMap[s].fScore;
}
if(stuMap[s].g >= 60){
stuVec.push_back(stuMap[s]);
num++;
}
}
}
sort(stuVec.begin(), stuVec.end(), cmp);
for(int i = 0; i < num; i++){
printf("%s %d %d %d %d\n",stuVec[i].id.c_str(),stuVec[i].pScore, stuVec[i].mScore, stuVec[i].fScore, stuVec[i].g);
}
return 0;
}
新的操作
map<string,node> sMap;
vector<node> sVec;
可能为空的 -1;
一开始就设置它 为 -1;有值再改;
map.count(s)
s在map里则返回 1;
即可以 if(map.count(s)){ };
自己的暴力硬做版本
#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
struct node{
string id;
int pScore, mScore, fScore, G;
};
map<string, int> mP;
map<string, int> mM;
map<string, int> mF;
map<string, int> mMvis;
map<string, int> mFvis;
bool cmp(node a, node b){
if(a.G != b.G) return a.G > b.G;
else return a.id < b.id;
}
int main(){
int p, m, n;
scanf("%d%d%d", &p, &m,&n);
for(int i = 0; i < p; i++){
string id;
int score;
cin>>id;
scanf("%d",&score);
if(score >= 200) mP[id] = score;
//小于200 的不考虑
}
for(int i = 0; i < m; i++){
string id;
int score;
cin>>id;
scanf("%d",&score);
if(mP.count(id)) {
mM[id] = score;
mMvis[id] = 1;
};
//编程分数 小于200 的不考虑
}
for(int i = 0; i < n; i++){
string id;
int score;
cin>>id;
scanf("%d",&score);
if(mP.count(id)){
mF[id] = score;
mFvis[id] = 1;
}
//编程分数 小于200 的不考虑
}
vector<node> ans;
double gScore;
int num = 0;
for(auto it = mP.begin(); it != mP.end(); it++){
if(mM[it->first] > mF[it->first]){
gScore = mM[it->first] * 0.4 + mF[it->first] * 0.6;
}else gScore = mF[it->first];
if(gScore >= 60){
num++;
ans.push_back(node{it->first,mP[it->first],mM[it->first],mF[it->first],(int)(gScore + 0.5)});
}
}
sort(ans.begin(), ans.end(), cmp);
//printf("%d\n",num);
for(int i = 0; i < ans.size(); i++){
cout<< ans[i].id;
printf(" %d",ans[i].pScore);
if(mMvis[ans[i].id]){
printf(" %d",ans[i].mScore);
}else printf(" -1");
if(mFvis[ans[i].id]){
printf(" %d",ans[i].fScore);
}else printf(" -1");
printf(" %d\n",ans[i].G);
}
return 0;
}