个人学习记录,代码难免不尽人意。
Sample Input 1:
3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95
Sample Output 1:
Mary EE990830
Joe Math990112
6
Sample Input 2:
1
Jean M AA980920 60
Sample Output 2:
Absent
Jean AA980920
NA
#include<cstdio>
#include<iostream>
#include<queue>
#include<map>
#include<string>
using namespace std;
struct node{string name;char gender;string id;int grade;node(string _name,char _gender,string _id,int _grade):name(_name),gender(_gender),id(_id),grade(_grade){}
};
queue<node> boys,girls;
int main(){int n;scanf("%d",&n);for(int i=0;i<n;i++){string name;char gender;string id;int grade;cin >> name >> gender >> id >> grade;if(gender=='F') girls.push(node(name,gender,id,grade));//不能写"F"else boys.push(node(name,gender,id,grade));}node m=node("",'M',"",1000000000);node f=node("",'F',"",0);bool mflag=true,fflag=true;if(!boys.empty()){while(!boys.empty()) {node no=boys.front();boys.pop();if(no.grade<m.grade){m=no;}}}else{mflag=false;}if(!girls.empty()){while(!girls.empty()){node no=girls.front();girls.pop();if(no.grade>f.grade){f=no;}}}else{fflag=false;}if(fflag){cout << f.name << " " << f.id <<endl; }else cout << "Absent" << endl;if(mflag){cout << m.name << " "<< m.id << endl;}else cout << "Absent" << endl;if(mflag&&fflag)printf("%d",f.grade-m.grade);else printf("NA");
}
比较简单,做题做多了会发现有的时候多用结构体和队列等数据结构要比单纯用数组来做逻辑更加清晰,更容易理出思路。
《算法笔记》上用char数组来存储name和id,但是我们用string就更加方便,不容易出错(char数组需要多加一位来存储\n)
除此之外,我还学习到需要用单引号来修饰单个字符,不能用双引号否则会出错。