PAT Talent and Virtue (25)
题目简介:
给出一些人的智商和美德值,按照以下标准排序:
1)排序的人智商和美德值都要大于等于一个底线L;
2)两个值都大于等于H的是圣人;
3)除去圣人后,剩余的人中智商小于H并且美德值大于等于H的是君子;
4)除去君子后,生于人中两值都小于H并且美德值大于等于智商值的是愚人;
5)剩余的两值都大于等于L的算是其他人;
6)排序规则:总分数非递增,总分一样美德值非递增,总分和美德值都一样那就序号(id)递增;
解题思路:
使用STL的list和vector相结合,按照上面1-6的顺序进行筛选。这里有个方便的函数remove_if可以使用,大大减少了代码量。list中的void remove_if(bool (*judgeFunction)(ElementType))函数可以删除调用judgeFunction函数返回为true的元素。
代码:
1 #include<iostream> 2 #include<list> 3 #include<vector> 4 #include<algorithm> 5 using namespace std; 6 struct People 7 { 8 int id; 9 int talent; 10 int virtue; 11 int total; 12 bool operator <(const People& a)const 13 { 14 if(this->total==a.total&&this->virtue==a.virtue) 15 return this->id<a.id; 16 if(this->total==a.total) 17 return this->virtue>a.virtue; 18 return this->total>a.total; 19 } 20 }; 21 int l,h; 22 vector<People> sages,noblemen,foolmen,others; 23 bool ilegal(const People& p) 24 { 25 return p.talent<l||p.virtue<l; 26 } 27 bool isSage(const People& p) 28 { 29 if(p.talent>=h&&p.virtue>=h) 30 { 31 sages.push_back(p); 32 return true; 33 } 34 return false; 35 } 36 bool isNobleman(const People& p) 37 { 38 if(p.talent<h&&p.virtue>=h) 39 { 40 noblemen.push_back(p); 41 return true; 42 } 43 return false; 44 } 45 bool isFoolman(const People& p) 46 { 47 if(p.talent<h&&p.virtue<h&&p.virtue>=p.talent) 48 { 49 foolmen.push_back(p); 50 return true; 51 } 52 return false; 53 } 54 bool isOther(const People& p) 55 { 56 if(p.talent>=l&&p.virtue>=l) 57 { 58 others.push_back(p); 59 return true; 60 } 61 return false; 62 } 63 template<class Begin> 64 inline void show(Begin ele,Begin end) 65 { 66 for(;ele!=end;ele++) 67 { 68 printf("%d %d %d\n",ele->id,ele->virtue,ele->talent); 69 } 70 } 71 int main() 72 { 73 freopen("in.txt","r",stdin); 74 freopen("out.txt","w",stdout); 75 int n; 76 scanf("%d%d%d",&n,&l,&h); 77 int i=0; 78 People p; 79 list<People> r; 80 for(i=0;i<n;i++) 81 { 82 scanf("%d%d%d",&p.id,&p.virtue,&p.talent); 83 p.total=p.virtue+p.talent; 84 r.push_back(p); 85 } 86 r.remove_if(ilegal); 87 r.remove_if(isSage); 88 r.remove_if(isNobleman); 89 r.remove_if(isFoolman); 90 r.remove_if(isOther); 91 sort(sages.begin(),sages.end()); 92 sort(noblemen.begin(),noblemen.end()); 93 sort(foolmen.begin(),foolmen.end()); 94 sort(others.begin(),others.end()); 95 printf("%d\n",sages.size()+noblemen.size()+foolmen.size()+others.size()); 96 show(sages.begin(),sages.end()); 97 show(noblemen.begin(),noblemen.end()); 98 show(foolmen.begin(),foolmen.end()); 99 show(others.begin(),others.end()); 100 return 0; 101 }

浙公网安备 33010602011771号