PAT甲级1062 Talent and Virtue (25分)|C++实现

一、题目描述

原题链接
在这里插入图片描述

Input Specification:

在这里插入图片描述

​​Output Specification:

在这里插入图片描述

Sample Input:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

Sample Output:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

二、解题思路

一道很经典的结构体排序题,根据题目的要求,我们设置四个vector<Student>来存放圣人、君子、愚人和小人,随后分别进行排序即可。

三、AC代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
struct Student
{
    
    
  long long id;
  int talent, virtue;
};
vector<Student> sage, noble, fool, small;
bool cmp(Student a, Student b)
{
    
    
  if(a.virtue+a.talent != b.virtue+b.talent)	return a.virtue+a.talent > b.virtue+b.talent;
  else if(a.virtue != b.virtue)	return a.virtue > b.virtue;
  else	return a.id < b.id;
}
void Sort()
{
    
    
  sort(sage.begin(), sage.end(), cmp);
  sort(noble.begin(), noble.end(), cmp);
  sort(fool.begin(), fool.end(), cmp);
  sort(small.begin(), small.end(), cmp);
}
void Print(vector<Student> a)
{
    
    
  for(int i=0; i<a.size(); i++)
    printf("%08lld %d %d\n", a[i].id, a[i].virtue, a[i].talent);
}
int main()
{
    
    
  int N, lower, high;
  vector<Student> all;
  Student tmp;
  int cnt = 0;
  scanf("%d%d%d", &N, &lower, &high);
  for(int i=0; i<N; i++)
  {
    
    
    scanf("%lld%d%d", &tmp.id, &tmp.virtue, &tmp.talent);
    if(tmp.virtue < lower || tmp.talent < lower)	continue;
    if(tmp.virtue >= high && tmp.talent >= high)	sage.push_back(tmp);
    else if(tmp.virtue >= high && tmp.talent < high)	noble.push_back(tmp);
    else if(tmp.virtue >= tmp.talent)	fool.push_back(tmp);
    else	small.push_back(tmp);
    cnt++;
  }
  printf("%d\n", cnt);
  Sort();
  Print(sage);
  Print(noble);
  Print(fool);
  Print(small);
  return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42393947/article/details/108658841