1062 Talent and Virtue (25 分)

根据司马光的理论,本篇探讨了人才与德行的重要性,并根据这两方面对人们进行分类与排名,包括圣人、君子、愚人与小人的定义与区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1062 Talent and Virtue (25 分)

About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and virtue. According to his theory, a man being outstanding in both talent and virtue must be a "sage(圣人)"; being less excellent but with one's virtue outweighs talent can be called a "nobleman(君子)"; being good in neither is a "fool man(愚人)"; yet a fool man is better than a "small man(小人)" who prefers talent than virtue.

Now given the grades of talent and virtue of a group of people, you are supposed to rank them according to Sima Guang's theory.

Input Specification:

Each input file contains one test case. Each case first gives 3 positive integers in a line: N (≤10​5​​), the total number of people to be ranked; L (≥60), the lower bound of the qualified grades -- that is, only the ones whose grades of talent and virtue are both not below this line will be ranked; and H (<100), the higher line of qualification -- that is, those with both grades not below this line are considered as the "sages", and will be ranked in non-increasing order according to their total grades. Those with talent grades below Hbut virtue grades not are cosidered as the "noblemen", and are also ranked in non-increasing order according to their total grades, but they are listed after the "sages". Those with both grades below H, but with virtue not lower than talent are considered as the "fool men". They are ranked in the same way but after the "noblemen". The rest of people whose grades both pass the L line are ranked after the "fool men".

Then N lines follow, each gives the information of a person in the format:

ID_Number Virtue_Grade Talent_Grade

where ID_Number is an 8-digit number, and both grades are integers in [0, 100]. All the numbers are separated by a space.

Output Specification:

The first line of output must give M (≤N), the total number of people that are actually ranked. Then M lines follow, each gives the information of a person in the same format as the input, according to the ranking rules. If there is a tie of the total grade, they must be ranked with respect to their virtue grades in non-increasing order. If there is still a tie, then output in increasing order of their ID's.

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

/*
解题思路:
1.德分和才分<L,为不及格,第5类考生
2.德分和才分都>=H,为第1类考生
3.才分<H,但德分>=H,为第2类考生
4.L<=德分才分均<H,且德分>=才分,为第3类考生
5.其余情况为第4类考生,这类包含情况较多,因此放在最后鉴别,可以减少代码量

排序:
1.先按类别从小到大排序
2.类别相同的,按总分从大到小排序
3.总分相同的,按德分从大到小排序
4.德分相同的,按准考证号从小到大排序

*/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

struct student{
    char id[10]; //准考证号
    int de,cai,sum; //德分,才分,总分
    int flag; //考生类别:第1类~第5类

}stu[100010];

bool cmp(student a,student b)
{
    if(a.flag!=b.flag) return a.flag<b.flag;//按类别从小到大
    else if(a.sum!=b.sum) return a.sum>b.sum; //类别相同的,按总分从大到小排序
    else if(a.de!=b.de) return a.de>b.de; //总分相同的,按德分从大到小排序
    else return strcmp(a.id,b.id)<0; //德分相同的,按准考证号从小到大排序

}


int main()
{
    int N,L,H; //N为考生总数,L为最低录取分数线,K为最优录取分数线
    scanf("%d%d%d",&N,&L,&H);
    int m=N; //m为及格人数
    for(int i=0;i<N;i++)
    {
        scanf("%s%d%d",stu[i].id,&stu[i].de,&stu[i].cai);
        stu[i].sum=stu[i].de+stu[i].cai; //计算总分
        if(stu[i].de<L||stu[i].cai<L)
        {
            stu[i].flag=5;
            m--; //及格人数减一
        }
        else if(stu[i].de>=H&&stu[i].cai>=H) stu[i].flag=1;
        else if(stu[i].de>=H&&stu[i].cai<H) stu[i].flag=2;
        else if(stu[i].de<H&&stu[i].cai<H&&stu[i].de>=stu[i].cai) stu[i].flag=3;
        else stu[i].flag=4;

    }

    sort(stu,stu+N,cmp); //排序
    printf("%d\n",m); //输出及格人数
    for(int i=0;i<m;i++)
    {
        printf("%s %d %d\n",stu[i].id,stu[i].de,stu[i].cai);
    }
    return 0;
}
 

 

 

### C++ 中的虚函数概念 #### 定义与作用 在面向对象编程中,为了实现多态机制,C++引入了虚函数的概念。通过关键字`virtual`声明成员函数为虚函数之后,允许派生类重写此方法并动态绑定到具体实例上[^1]。 #### 声明方式 要在基类中定义一个普通的虚函数,只需简单地在其前加上`virtual`修饰符即可: ```cpp class Base { public: virtual void show() { cout << "Base class"; } }; ``` 对于派生类而言,则可以通过覆盖(即使用相同的签名重新定义)这些被标记成虚拟的方法来改变行为: ```cpp class Derived : public Base{ public: void show() override{cout<<"Derived class";} }; ``` 这里需要注意的是,在现代C++标准里推荐显式指定`override`以表明意图并且防止拼写错误引发的问题[^2]。 #### 动态派过程解析 每当创建含有至少一个虚函数的对象时,编译器会自动为其配一块隐藏区域用于保存指向_vtable_(虚函数表)的指针。而_vptr_(虚表指针)则始终位于对象内存布局最前端位置以便快速访问。当调用某个可能涉及继承体系内不同版本的选择时,程序运行期间将会依据当前实际类型的_vptr_所指示的位置去查找对应的目标地址完成最终跳转操作[^3]。 ```cpp #include <iostream> using namespace std; // 定义基类 class Animal { protected: string name; public: explicit Animal(const char* n):name(n){} virtual ~Animal(){} virtual void speak(){cout<<this->name<<" makes a sound.";} }; // 继承自基类的新类型 class Dog : public Animal { public: using Animal::Animal; // 使用父级构造参数初始化列表简化语法糖 void speak() override { cout << this->name << " barks loudly."; } }; int main(){ auto p_animal = new Dog("Rex"); p_animal->speak(); // 输出:"Rex barks loudly." delete p_animal; return 0; } ``` 上述例子展示了如何利用虚函数特性让同一接口表达出多种不同的语义逻辑,从而达到更加灵活的设计目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值