PAT-B 1015. 德才论(同PAT 1062. Talent and Virtue)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT-B 1015. 德才论(同PAT 1062. Talent and Virtue)相关的知识,希望对你有一定的参考价值。
1. 在排序的过程中,注意边界的处理(小于、小于等于)
2. 对于B-level,这题是比較麻烦一些了。
源代码:
#include <cstdio> #include <vector> #include <algorithm> using namespace std; struct People { int m_id; int m_virtue; int m_talent; People(int id, int virtue, int talent): m_id(id), m_virtue(virtue), m_talent(talent) {} void print() { printf("%08d %d %d\n", m_id, m_virtue, m_talent); } friend bool operator< (const People& a, const People& b) { if (a.m_virtue+a.m_talent != b.m_virtue+b.m_talent) { return a.m_virtue+a.m_talent > b.m_virtue+b.m_talent; } else if (a.m_virtue != b.m_virtue) { return a.m_virtue > b.m_virtue; } else { return a.m_id < b.m_id; } } }; vector<People> sage, noble_man, fool_man, small_man; int n, l, h; int id, virtue, talent; int main() { scanf("%d%d%d", &n, &l, &h); for (int i = 0; i < n; ++ i) { scanf("%d%d%d", &id, &virtue, &talent); if (virtue < l || talent < l) { continue; } else if (virtue >= h && talent >= h) { sage.push_back( People(id, virtue, talent) ); } else if (virtue >= h) { noble_man.push_back( People(id, virtue, talent) ); } else if (virtue >= talent) { fool_man.push_back( People(id, virtue, talent) ); } else { small_man.push_back( People(id, virtue, talent) ); } } sort(sage.begin(), sage.end()); sort(noble_man.begin(), noble_man.end()); sort(fool_man.begin(), fool_man.end()); sort(small_man.begin(), small_man.end()); printf("%d\n", sage.size() + noble_man.size() + fool_man.size() + small_man.size()); for (size_t i = 0; i < sage.size(); ++ i) { sage[i].print(); } for (size_t i = 0; i < noble_man.size(); ++ i) { noble_man[i].print(); } for (size_t i = 0; i < fool_man.size(); ++ i) { fool_man[i].print(); } for (size_t i = 0; i < small_man.size(); ++ i) { small_man[i].print(); } return 0; }
以上是关于PAT-B 1015. 德才论(同PAT 1062. Talent and Virtue)的主要内容,如果未能解决你的问题,请参考以下文章