首页 > 试题广场 >

Talent and Virtue (25)

[编程题]Talent and Virtue (25)
  • 热度指数:1493 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
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.

输入描述:
Each input file contains one test case. Each case first gives 3 positive integers in a line: N (<=105), 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
H but 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.


输出描述:
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.
示例1

输入

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

输出

12<br/>10000013 90 99<br/>10000012 80 100<br/>10000003 85 80<br/>10000011 85 80<br/>10000004 80 85<br/>10000007 90 78<br/>10000006 83 76<br/>10000005 82 77<br/>10000002 90 60<br/>10000014 66 60<br/>10000008 75 79<br/>10000001 64 90
本题主要考察排序。
我的解法是将四种不同的类型根据条件存储在不同的数组中,然后分别排序。
看到一位同学的解法,颇有点巧妙:也是根据条件分组,不过按照圣人、君子、愚人、小人分别加了600、400、200、0等,然后整体排序。
发表于 2015-07-01 23:26:30 回复(1)
#include <bits/stdc++.h>  #define debug using namespace std; int lb, ub; typedef struct person { string id; int a, b, sum; person(string s, int a, int b) : id(s), a(a), b(b), sum(a + b) {
    }; void print() { cout << id << " " << a << " " << b << endl;
    } int classOfPerson() { if (a >= ub && b >= ub) return 0;//sage  if (a >= ub && b < ub)return 1;//noblemen  if (a < ub && a >= b)return 2;//foolmen  return 3;
    }
} person; bool cmp(person p1, person p2) { int c1 = p1.classOfPerson(), c2 = p2.classOfPerson(); if (c1 != c2) return c1 < c2; if (p1.sum != p2.sum) return p1.sum > p2.sum; if (p1.a != p2.a) return p1.a > p2.a; return p1.id < p2.id;
} int main() { #ifdef debug  freopen("input.txt", "r", stdin); //        freopen("output.txt","w",stdout); #endif   vector<person> all; int n, a, b, k = 0; string id; cin >> n >> lb >> ub; for (int i = 0; i < n; ++i) { cin >> id >> a >> b; if (a < lb || b < lb) { continue; } k++; all.emplace_back(id, a, b);
    } cout << k << endl;
    sort(all.begin(), all.end(), cmp); for (int i = 0; i < all.size(); ++i) { all[i].print();
    } return 0;
};
发表于 2021-03-11 10:26:50 回复(0)
#pragma warning (disable: 4996)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;

//对于排序对象:n 个记录序列{R1,R2,...,Rn}, Rj的关键字字段为{Kj(0),Kj(1),...,Kj(d-1)}优先级从高到低
//可以采取低位优先排序策略LSD(Least Significant Digit first):
//第1趟,对整个序列,根据Kj(d-1)进行稳定排序
//第2趟,对整个序列,根据Kj(d-2)进行稳定排序
//...
//第i趟,对整个序列,根据Kj(d-i)进行稳定排序
//...
//第d趟,对整个序列,根据Kj(0)进行稳定排序
//因为越后作为排序根据的字段,优先级越高,所以排序结果正确
//稳定排序可以选择:插入排序O(n^2),桶排序O(n);若选择桶排序,则LSD也叫做基数排序

//本题每个记录Rj只有4个关键字字段{Type(圣人<...<小人),Grade=Virtue+Talent,Virtue,-ID}优先级从高到低
//ID因为有8位数,桶数组过大,采取插入排序;其余采取桶排序
//按上述思路排序完之后,正好与题述要求相反,逆序输出即可

#define KEY_NUM 4
#define Radix 1000 //桶数组最大可能容量,也就是关键字字段取值范围的最大值,Grade取值范围最大,为200,这里索性设置为1000
#define MAX_SIZE 100000

int N, L, H; int M;
int ID, Virtue, Talent, Grade, Type;
typedef struct LNode {
	int key[KEY_NUM];
	struct LNode* next;
}LNode;

LNode* A = new LNode; //把数据结构设计成链表的优点可以自行体会

typedef struct Bucket {
	LNode* front; 
	LNode* rear;
}Bucket;//每个桶是一个队列

Bucket ht[Radix];//桶数组,也就是散列表的实际载体

void InsertSort(int i) {//链表插入排序,根据Rj的关键字字段Kj(i)
	for (LNode* p = A->next; p && p->next;) {
		LNode* y = p->next;
		LNode* q = A;
		while (q->next != y && q->next->key[i] <= y->key[i])q = q->next;
		p->next = y->next; 
		y->next = q->next; 
		q->next = y; 
		if (q == p)p = p->next;
	}
}
void BucketSort(int i) {//第i趟桶排序
	//初始化桶数组
	for (int j = 0; j < Radix; ++j)ht[j].front = ht[j].rear = NULL;
	//散列映射
	for (LNode* p = A->next; p; p = p->next) {//对于每个记录
		int Ki = p->key[i];
		if (!ht[Ki].front)ht[Ki].front = ht[Ki].rear = p;//桶空则插入空桶
		else { ht[Ki].rear->next = p; ht[Ki].rear = p; }//桶不空则接入桶的末尾(独立链法)
	}
	//整理序列
	int Ki; LNode* p = A;
	for (Ki = 0; Ki < Radix; ++Ki) {
		if (ht[Ki].front) {
			p->next = ht[Ki].front;
			p = ht[Ki].rear;
		}
	}
	p->next = NULL;
}


int main()
{
	cin >> N >> L >> H; M = 0;
	A->next = NULL; LNode* p = A;
	for (int i = 0; i < N; ++i) {
		cin >> ID >> Virtue >> Talent; Grade = Virtue + Talent;
		if (!(Virtue < L || Talent < L)) {
			if (Virtue >= H && Talent >= H)
				Type = 3;
			else if (Virtue >= H && Talent < H) Type = 2;
			else if (Virtue < H && Talent < H && Virtue >= Talent) Type = 1;
			else Type = 0;
			LNode* x = new LNode;
			x->key[0] = Type;//要求圣人<君子<蠢人<小人,而实际Type是:圣人>君子>蠢人>小人
			x->key[1] = Grade;//要求从大到小排序,实际为从小到大排序
			x->key[2] = Virtue;//要求从大到小排序,实际为从小到大排序
			x->key[3] = -ID;//ID要求从小到大排序,取负后,变为从大到小排序(综上所述,排序完的逆序为题目所求)
			x->next = NULL;
			p->next = x; p = p->next;
			++M;
		}
	}

	InsertSort(3);
	/* 如果都用插入排序会超时
	InsertSort(2);
	InsertSort(1);
	InsertSort(0);
	*/
	//基数排序=逆优先级顺序地桶排序
	BucketSort(2);
	BucketSort(1);
	BucketSort(0);
	cout << M << endl;
	vector<LNode*> res;
	for (p = A->next; p; p = p->next) res.push_back(p);
	while (M > 0) {//逆序输出结果
		cout << -res.back()->key[3] << " " << res.back()->key[2] << " " << res.back()->key[1] - res.back()->key[2];
		if (M > 1)cout << endl;
		res.pop_back(); M--;
	}
	return 0;
}


发表于 2021-01-07 22:13:56 回复(0)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

//1062 Talent and Virtue
//难点就在于题干跟input的描述不一样。
//题干说one's virtue outweighs talent can be called a "nobleman"
//然后input里面又说只要V>=H,T<H就行。
//题干里没有对fool作要求,input里要求v>=t才能是fool。

struct person{
    string id;
    int v,t,total;
};

vector<person> sage;
vector<person> noble;
vector<person> fool;
vector<person> small;

bool mycomp(const person &a,const person &b){
    if(a.total!=b.total){
        return a.total>b.total;
    }
    else if(a.v!=b.v){
        return a.v>b.v;
    }
    else{
        return a.id<b.id;
    }
}

int main(){
    int n,l,h;
    cin>>n>>l>>h;
    
    string id;int v,t,cnt=0;
    for(int i=0;i<n;++i){
        cin>>id>>v>>t;
        if(v<l || t<l) continue;
        cnt++;
        if(v>=h && t>=h){
            sage.push_back({id,v,t,v+t});
        }
        else if(v>=h && t<h){
            noble.push_back({id,v,t,v+t});
        }
        else if(v<h && t<h && v>=t){ //别忘了 virtue>=talent的限制
            fool.push_back({id,v,t,v+t});
        }
        else{
            small.push_back({id,v,t,v+t});
        }
    }
    
    sort(sage.begin(),sage.end(),mycomp);
    sort(noble.begin(),noble.end(),mycomp);
    sort(fool.begin(),fool.end(),mycomp);
    sort(small.begin(),small.end(),mycomp);
    
    cout<<cnt<<endl;
    
    for(vector<person>::iterator i =sage.begin();i!=sage.end();++i){
        cout<<i->id<<" "<<i->v<<" "<<i->t<<endl;
    }
    for(vector<person>::iterator i =noble.begin();i!=noble.end();++i){
        cout<<i->id<<" "<<i->v<<" "<<i->t<<endl;
    }
    for(vector<person>::iterator i =fool.begin();i!=fool.end();++i){
        cout<<i->id<<" "<<i->v<<" "<<i->t<<endl;
    }
    for(vector<person>::iterator i =small.begin();i!=small.end();++i){
        cout<<i->id<<" "<<i->v<<" "<<i->t<<endl;
    }
    
    
    return 0;
}
发表于 2019-08-24 14:17:13 回复(0)
N,L,H=map(int,input().split())
indata,res=[list(map(int,input().split())) for _ in range(N)],[]
for e in indata:
    if e[1]>=L and e[2]>=L:
        e+=[1] if e[1]>=H and e[2]>=H else [2] if e[1]>=H else [3] if e[1]>=e[2] else [4]
        res.append(e)
res.sort(key=lambda x:(x[3],-x[1]-x[2],-x[1],x[0]))
print(len(res))
for e in res:
    print(' '.join(map(str,e[:3])))

编辑于 2018-09-06 09:24:42 回复(2)
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, L, H;
struct People
{
    string ID;
    int Virtue_Grade, Talent_Grade, Total_Grade;
    int Type; // 4:sage 3:nobleman 2:foolman 1:smallman
    People(string sid, int vg, int tg)
        : ID(sid), Virtue_Grade(vg), Talent_Grade(tg) {}
    void set_type()
    {
        Total_Grade = Virtue_Grade + Talent_Grade;
        if ((Talent_Grade >= H) && (Virtue_Grade >= H))
            Type = 4;
        else if ((Talent_Grade) < H && (Virtue_Grade >= H))
            Type = 3;
        else if ((Talent_Grade < H) && (Virtue_Grade < H) &&
                 (Virtue_Grade >= Talent_Grade))
            Type = 2;
        else
            Type = 1;
    }
    void show()
    {
        cout << ID << " " << Virtue_Grade << " " << Talent_Grade << endl;
    }
};
vector<People> Popu;

bool compare(People p1, People p2)
{
    if (p1.Type != p2.Type)
        return p1.Type > p2.Type;
    else
    {
        if (p1.Total_Grade != p2.Total_Grade)
            return p1.Total_Grade > p2.Total_Grade;
        else
        {
            if (p1.Virtue_Grade != p2.Virtue_Grade)
            {
                return p1.Virtue_Grade > p2.Virtue_Grade;
            }
            else
            {
                return p1.ID < p2.ID;
            }
        }
    }
}

int main()
{
    scanf("%d %d %d", &n, &L, &H);
    for (int i = 0; i < n; i++)
    {
        string ID_Number;
        int virtue, talent;
        cin >> ID_Number >> virtue >> talent;
        if (virtue < L || talent < L)
            continue;
        People s(ID_Number, virtue, talent);
        s.set_type();
        Popu.push_back(s);
    }
    sort(Popu.begin(), Popu.end(), compare);
    cout << Popu.size() << endl;
    for (auto iter = Popu.begin(); iter != Popu.end(); iter++)
        iter->show();
    return 0;
}

发表于 2017-09-05 15:21:05 回复(0)
//先分个等级
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<stdio.h>
#include<vector>
using namespace std;
intN,M,K;
struct People
{
    intcode;
    intvertue;
    inttalent;
    intlevel;
};
bool cmp(People a,People b)
{
    if(a.level!=b.level)
    {
        returna.level<b.level;
    }
    else
    {
        if(a.vertue+a.talent!=b.vertue+b.talent)
            returna.vertue+a.talent>b.vertue+b.talent;
        else
        {
            if(a.vertue!=b.vertue)
                returna.vertue>b.vertue;
            else
            {
                returna.code<b.code;
            }
        }
    }
}
vector<People>v;
intmain()
{
 
    cin>>N>>M>>K;
 
    for(inti=0;i<N;i++)
    {
        People p;
        scanf("%d%d%d",&p.code,&p.vertue,&p.talent);
        if(p.vertue>=K&&p.talent>=K)
            p.level=0;
        elseif(p.vertue>=K&&p.talent>=M&&p.talent<K)
            p.level=1;
        elseif(p.vertue>=M&&p.vertue<K&&p.talent>=M&&p.vertue>=p.talent)
            p.level=2;
        elseif(p.vertue>=M&&p.vertue<K&&p.talent>=M&&p.vertue<p.talent)
            p.level=3;
        elsecontinue;
        v.push_back(p);
    }
    sort(v.begin(),v.end(),cmp);
    cout<<v.size()<<endl;
    for(inti=0;i<v.size();i++)
    {
        printf("%08d %d %d\n",v[i].code,v[i].vertue,v[i].talent);
        /*if(i!=v.size()-1)
            printf("\n");*/
    }
    return0;
}
发表于 2016-12-04 11:47:05 回复(0)
//按照美德排相同,再按ID,排相同
//分4类
//补充:字符串有两种比较方式,一种是< ,> :逐个比较单词的ASC码
// 另一种是 strcmp(int [],int []): 比较字符串长度
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

const int N=1e5+10;

struct W{
    string a;
    int t;
    int v;
    int all;
    bool operator<(const W&w){
        if(w.all!=all) return w.all<all;
        else {
            if(w.v==v) return w.a>=a;
            else return w.v<v;
        }
    }
}sage[N];

struct LOWER{
    string a;
    int t;
    int v;
    int all;
    bool operator<(const LOWER&w){
        if(w.all!=all) return w.all<all;
        else {
            if(w.v==v) return w.a>=a;
            else return w.v<v;
        }
    }
}noblemen[N];

struct LOWEST{
    string a;
    int t;
    int v;
    int all;
    bool operator<(const LOWEST&w){
        if(w.all!=all) return w.all<all;
        else {
            if(w.v==v) return w.a>=a;
            else return w.v<v;
        }
    }
}foolman[N];

struct LOWES{
    string a;
    int t;
    int v;
    int all;
    bool operator<(const LOWES&w){
        if(w.all!=all) return w.all<all;
        else {
            if(w.v==v) return w.a>=a;
            else return w.v<v;
        }
    }
}smallman[N];

int main(){
    int idxs=0,idxn=0,idxf=0,idxx=0;
    int n,H,L;
    cin>>n>>L>>H;
    string s;
    int a,b;
    while(n--){
        cin>>s>>a>>b;
        if(a>=L&&b>=L){
            if(a>=H&&b>=H){//sage
                sage[idxs++]={s,b,a,b+a};
            }
            else if(a>=H&&b<H){ //君子
                noblemen[idxn++]={s,b,a,b+a};
            }
            else {
                if(a>=b){  //愚人
                    foolman[idxf++]={s,b,a,b+a};
                }
                else{  //小人
                    smallman[idxx++]={s,b,a,b+a};
                }
            }
        }
    }
    sort(sage, sage+idxs);
    sort(noblemen,noblemen+idxn);
    sort(foolman,foolman+idxf);
    sort(smallman,smallman+idxx);
    
    cout<<idxs+idxx+idxf+idxn<<endl;
    for(int i=0;i<idxs;i++) cout<<sage[i].a<<" "<<sage[i].v<<" "<<sage[i].t<<endl;
    for(int i=0;i<idxn;i++) cout<<noblemen[i].a<<" "<<noblemen[i].v<<" "<<noblemen[i].t<<endl;
    for(int i=0;i<idxf;i++) cout<<foolman[i].a<<" "<<foolman[i].v<<" "<<foolman[i].t<<endl;
    for(int i=0;i<idxx;i++) cout<<smallman[i].a<<" "<<smallman[i].v<<" "<<smallman[i].t<<endl;
    
    return 0;
}

发表于 2020-12-29 18:50:47 回复(0)
#include<iostream>
(720)#include<vector>
#include<algorithm>
using namespace std;
struct person {
	int id;
	int virtue;
	int talent;
	person(int id, int virtue, int talent) :id(id), virtue(virtue), talent(talent) {}
};
bool cmp(struct person a, struct person 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;
	}
}
int main() {
	int N, L, H;
	cin >> N >> L >> H;
	vector<person>sage;
	vector<person>nobleman;
	vector<person>foolman;
	vector<person>others;
	for (int i = 0; i < N; i++) {
		int id, virtue, talent;
		cin >> id >> virtue >> talent;
		if (virtue >= H && talent >= H) {
			sage.push_back(person(id, virtue, talent));
		}
		else if (virtue >= H && talent < H&&talent>=L) {
			nobleman.push_back(person(id, virtue, talent));
		}
		else if (virtue < H && talent < H &&virtue>=L &&talent>=L && virtue >= talent) {
			foolman.push_back(person(id, virtue, talent));
		}
		else if (virtue >= L && talent >= L) {
			others.push_back(person(id, virtue, talent));
		}
	}
	printf("%d\n", sage.size() + nobleman.size() + foolman.size() + others.size());
	sort(sage.begin(), sage.end(), cmp);
	sort(nobleman.begin(), nobleman.end(), cmp);
	sort(foolman.begin(), foolman.end(), cmp);
	sort(others.begin(), others.end(), cmp);
	for (int i = 0; i < sage.size(); i++) {
		printf("%08d %d %d", sage[i].id, sage[i].virtue, sage[i].talent);
			printf("\n");
	}
	for (int i = 0; i < nobleman.size(); i++) {
		printf("%08d %d %d", nobleman[i].id, nobleman[i].virtue, nobleman[i].talent);
			printf("\n");
	}
	for (int i = 0; i < foolman.size(); i++) {
		printf("%08d %d %d", foolman[i].id, foolman[i].virtue, foolman[i].talent);
			printf("\n");
	}
	for (int i = 0; i < others.size(); i++) {
		printf("%08d %d %d", others[i].id, others[i].virtue, others[i].talent);
			printf("\n");
	}
}

发表于 2020-03-27 17:15:36 回复(0)
//其实就是对号入座,分别排序
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct Person {
    int id, v_grade, t_grade, total;
};
bool cmp(const Person& p1, const Person& p2) {
    if (p1.total != p2.total)
        return p1.total > p2.total;
    else {
        if (p1.v_grade != p2.v_grade)
            return p1.v_grade > p2.v_grade;
        else 
            return p1.id < p2.id;
    }
}
int main() {
    int n, l, h;
    cin >> n >> l >> h;
    int p_id, gv, gt,sum;
    vector<vector<Person>>  person(4);
    for (int i = 0; i < n; i++) {
        cin >> p_id >> gv >> gt;
        sum = gv + gt;
        if (gv < l || gt < l)
            continue;
        else {
            if (gv >= h) {
                if (gt >= h)
                    person[0].push_back({ p_id,gv,gt,sum });
                else
                    person[1].push_back({ p_id,gv,gt,sum });
            }
            else {
                if(gt<h && gv>=gt)
                    person[2].push_back({ p_id,gv,gt,sum });
                else
                    person[3].push_back({ p_id,gv,gt,sum });
            }
        }
    }
    int nsize = 0;
    for (int i = 0; i < person.size(); i++) {
        sort(person[i].begin(), person[i].end(), cmp);
        nsize += person[i].size();
    }
    printf("%d\n", nsize);
    for (int i = 0; i < person.size(); i++)
        for (int j = 0; j < person[i].size(); j++)
            printf("%08d %d %d\n", person[i][j].id, person[i][j].v_grade, person[i][j].t_grade);
    return 0;
}

发表于 2019-12-03 10:54:08 回复(0)
n,l,h = map(int,input().split())
lst,a,b,c,d = [],[],[],[],[]
for i in range(n):
    tem = list(map(int,input().split()))
    if tem[1]>=l and tem[2]>=l:
        if tem[1]>=h and tem[2]>=h:
            a.append(tem)
        elif tem[1]>=h and tem[2]<h:
            b.append(tem)
        elif tem[1]<h and tem[2]<h and tem[1]>=tem[2]:
            c.append(tem)
        else:
            d.append(tem)
a.sort(key=lambda x:(-(x[1]+x[2]),-x[1],x[0]))
b.sort(key=lambda x:(-(x[1]+x[2]),-x[1],x[0]))
c.sort(key=lambda x:(-(x[1]+x[2]),-x[1],x[0]))
d.sort(key=lambda x:(-(x[1]+x[2]),-x[1],x[0]))
lst = a+b+c+d
print(len(lst))
for i in lst:
    print(i[0],i[1],i[2])

发表于 2018-12-09 12:05:41 回复(0)

import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int l = sc.nextInt();
            int h = sc.nextInt();
            ArrayList<man> list = new ArrayList<>();
            for (int i = 0;i<n;i++){
                String name = sc.next();
                int vir = sc.nextInt();
                int tal = sc.nextInt();
                if (vir >= l && tal >= l) {
                    int grade = 0;
                    if (vir >= h && tal >= h) {
                        grade = 1;
                    } else if (vir >= h) {
                        grade = 2;
                    } else if (vir >= tal) {
                        grade = 3;
                    } else {
                        grade = 4;
                    }
                    list.add(new man(name,vir,tal,grade));
                }
            }
            Collections.sort(list, new Comparator<man>() {
                @Override
                public int compare(man o1, man o2) {
                    if (o1.grade != o2.grade){
                        return o1.grade - o2.grade;
                    }else if ((o2.virtue+o2.talent) != (o1.virtue+o1.talent)){
                        return (o2.virtue+o2.talent) - (o1.virtue+o1.talent);
                    }else if (o2.virtue != o1.virtue){
                        return o2.virtue - o1.virtue;
                    }else {
                        return o1.name.compareTo(o2.name);
                    }
                }
            });
            System.out.println(list.size());
            for (man m:list){
                System.out.println(m.name+" "+m.virtue+" "+m.talent);
            }
        }
    }
    static class man{
        String name;
        int virtue;
        int talent;
        int grade;
        public man(String name,int virtue,int talent,int grade){
            this.name = name;
            this.virtue = virtue;
            this.talent = talent;
            this.grade = grade;
        }
    }
}
发表于 2018-11-27 16:08:14 回复(0)
乙级题目https://www.nowcoder.com/profile/7457474/codeBookDetail?submissionId=28795198  
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
usingnamespacestd;
// 第一类 德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;
// 才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;
// 德才分均低于H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,
// 但排在第二类考生之后;
// 其他达到最低线L的考生也按总分排序,但排在第三类考生之后。
// 当某类考生中有多人总分相同时,按其德分降序排列;
// 若德分也并列,则按准考证号的升序输出。
typedefstruct
{
    string id;
    intscoreDe;
    intscoreCai;
    intsumScore;
}student;
intChooseClass(student &s,intH,intL)
{
    intclass_student = 0;
    if(s.scoreCai >= H && s.scoreDe >= H)
    {
        class_student = 1;
    }
    elseif(s.scoreCai >= L && s.scoreDe >= H)
    {
        class_student = 2;
    }
    elseif(s.scoreCai >= L && s.scoreDe >= L && s.scoreCai <= s.scoreDe)
    {
        class_student = 3;
    }
    elseif(s.scoreCai >= L && s.scoreDe >= L)
    {
        class_student = 4;
    }
    else
    {
        class_student = 5;
    }
    s.sumScore = s.scoreCai + s.scoreDe;
    returnclass_student;
}
 
boolcmp(student &a ,student &b)
{
    if(a.sumScore > b.sumScore)
    {
        returntrue;
    }
    elseif(a.sumScore == b.sumScore)
    {
        if(a.scoreDe > b.scoreDe)
        {
            returntrue;
        }
        elseif(a.scoreDe == b.scoreDe)
        {
            if(a.id < b.id)
            {
                returntrue;
            }
        }
    }
    returnfalse;
}
intmain()
{
    intN,L,H;
    cin >> N >> L >> H;
 
    string temp_id;
    inttempScoreDe;
    inttempScoreCai;
    vector<student> stu[5];
    student *pStudent =newstudent[N];
    for(inti=0; i<N; i++)
    {
        cin >> temp_id;
        cin >> tempScoreDe;
        cin >> tempScoreCai;
        pStudent[i].scoreCai = tempScoreCai;
        pStudent[i].scoreDe = tempScoreDe;
        pStudent[i].id = temp_id;
        stu[ChooseClass(pStudent[i], H, L)-1].push_back(pStudent[i]);
    }
    cout << stu[0].size() + stu[1].size() + stu[2].size() + stu[3].size() << endl;
    for(inti=0; i<4; i++){
        sort(stu[i].begin(),stu[i].end(),cmp);
        for(intj=0; j<stu[i].size(); j++)
        {
            cout << stu[i][j].id <<" "<< stu[i][j].scoreDe <<" "<< stu[i][j].scoreCai << endl;
        }
    }
 
}

发表于 2018-08-30 08:56:07 回复(0)
n, l, h = map(int, input().split())
men = [[],[],[],[]]
for t in range(n):
    i, vi, ta = map(int, input().split())
    if ta<l or vi<l: continue
    if ta>=h and vi>=h:
        men[0].append((i,vi,ta))
    elif ta<h<=vi:
        men[1].append((i,vi,ta))
    elif ta<=vi<h:
        men[2].append((i,vi,ta))
    else:
        men[3].append((i,vi,ta))

print(sum(len(x) for x in men))
for i in range(4):
    if not men[i]: continue
    ls = sorted(men[i], key=lambda t:(-t[1]-t[2], -t[1], t[0]))
    for x in ls:
        print(x[0], x[1], x[2])

发表于 2018-05-02 18:45:24 回复(0)
//  超出内存限制了。。。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
class Person{
	private int Id;
	private int Talent;
	private int Virtue;
	private int total;
	
	public Person(int Id,int Talent,int Virtue,int total){
		this.Id = Id;
		this.Talent = Talent;
		this.Virtue = Virtue;
		this.total = total;
	}
	public int getId(){
		return Id;
	}
	public int getTalent(){
		return Talent;
	}
	public int getVirtue(){
		return Virtue;
	}
	public int getTotal(){
		return total;
	}
}

class newsort implements Comparator{
	public int compare(Object o1,Object o2){
		Person p1 = (Person) o1;
		Person p2 = (Person) o2;

		
		if(p1.getTotal()<p2.getTotal()){  //p2在前p1在后
			return 1;
		}else if(p1.getTotal()<p2.getTotal()){
			return -1;
		}else if(p1.getTotal()==p2.getTotal()){           // 两者相等
			if(p1.getVirtue()<p2.getVirtue()){
				return 1;
			}else{
				return -1;
			}
		}else if(p1.getTotal()==p2.getTotal()&&p1.getVirtue()==p2.getVirtue()){
			if(p1.getId()>p2.getId()){
				return 1;
			}else{
				return -1;
			}
		}else{
			return -1;
		}
		
	}
}

public class Main {
	
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int L = in.nextInt();
		int H = in.nextInt();
		ArrayList<Person> sages = new ArrayList<Person>();
		ArrayList<Person> noblemen = new ArrayList<Person>();
		ArrayList<Person> foolmen = new ArrayList<Person>();
		ArrayList<Person> smallmen = new ArrayList<Person>();
		for(int i=0;i<N;i++){
			int ID = in.nextInt();
			int Virtue = in.nextInt();
			int Talent = in.nextInt();
			int Total = Virtue + Talent;
			Person person = new Person(ID,Talent,Virtue,Total);
			if(Talent>=H&&Virtue>=H){
				sages.add(person);
			}else if(Talent<H&&Talent>=L&&Virtue>=H){
				noblemen.add(person);
			}else if(Talent<H&&Talent>=L&&Virtue<H&&Virtue>=L&&Virtue>=Talent){
				foolmen.add(person);
			}else if(Talent>=L&&Virtue>=L){
				smallmen.add(person);
			}
		}
		// 排序
		Collections.sort(sages,new newsort());
		Collections.sort(noblemen,new newsort());
		Collections.sort(foolmen,new newsort());
		Collections.sort(smallmen,new newsort());
		
		System.out.println(sages.size()+noblemen.size()+foolmen.size()+smallmen.size());
		//System.out.println("sages");
		for(int i=0;i<sages.size();i++){
			System.out.print(sages.get(i).getId()+" ");
			System.out.print(sages.get(i).getVirtue()+" ");
			System.out.println(sages.get(i).getTalent());
		}
		
		//System.out.println("noblemen");
		for(int i=0;i<noblemen.size();i++){
			System.out.print(noblemen.get(i).getId()+" ");
			System.out.print(noblemen.get(i).getVirtue()+" ");
			System.out.println(noblemen.get(i).getTalent());
		}
		//System.out.println("foolmen");
		for(int i=0;i<foolmen.size();i++){
			System.out.print(foolmen.get(i).getId()+" ");
			System.out.print(foolmen.get(i).getVirtue()+" ");
			System.out.println(foolmen.get(i).getTalent());
		}
		//System.out.println("smallmen");
		for(int i=0;i<smallmen.size();i++){
			System.out.print(smallmen.get(i).getId()+" ");
			System.out.print(smallmen.get(i).getVirtue()+" ");
			System.out.println(smallmen.get(i).getTalent());
		}
		
	}
}

发表于 2017-06-28 19:07:01 回复(0)
先说下要注意的
1.所谓的below就是小于,但是pass是>=
2.不用判断重复ID,就算有重复作2个记录就是了
思路不难:
1.读入时刷掉任一数值小于L的,符合要求的压入person
2.此时可以输出person.size()了因为里面的全是要输出的
3.依次遍历person
   both>=H压入sages;
   virtue>=H压入noblemem;
   virtue>=talent压入fool
   其他压入rest
4.挨个排序并且输出就好
代码如下:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct Person {
	int Talent;
	int Virtue;
	int ID;
	Person(int _ID, int _Virtue, int _Talent) :ID(_ID), Virtue(_Virtue), Talent(_Talent) {}
};

bool cmp(Person a, Person b) {
	if (a.Talent + a.Virtue - b.Talent - b.Virtue)
		return a.Talent + a.Virtue > b.Talent + b.Virtue;
	if (a.Virtue - b.Virtue)return a.Virtue > b.Virtue;
	return a.ID < b.ID;
}

int main(int argc, const char* argv[]) {
	ios::sync_with_stdio(false);
	int N, L, H;
	cin >> N >> L >> H;
	vector<Person> person;
	for (int i = 0;i < N;i++) {
		int id, vir, tal;
		cin >> id >> vir >> tal;
		if (vir < L || tal < L)continue;
		person.push_back(Person(id, vir, tal));
	}
	vector<Person> sages;						//	both pass H
	vector<Person> noblemen;					//	vir pass H && tal below H
	vector<Person> fool;
	vector<Person> rest;
	for (int i = 0;i < person.size();i++) {
		if (person[i].Virtue >= H)
			if (person[i].Talent >= H)sages.push_back(person[i]);
			else noblemen.push_back(person[i]);
		else if (person[i].Talent <= person[i].Virtue)
			fool.push_back(person[i]);
		else rest.push_back(person[i]);
	}
	sort(sages.begin(), sages.end(), cmp);
	sort(noblemen.begin(), noblemen.end(), cmp);
	sort(fool.begin(), fool.end(), cmp);
	sort(rest.begin(), rest.end(), cmp);
	cout << person.size();
	for (int i = 0;i < sages.size();i++)
		cout << endl << sages[i].ID << " " << sages[i].Virtue << " " << sages[i].Talent;
	for (int i = 0;i < noblemen.size();i++)
		cout << endl << noblemen[i].ID << " " << noblemen[i].Virtue << " " << noblemen[i].Talent;
	for (int i = 0;i < fool.size();i++)
		cout << endl << fool[i].ID << " " << fool[i].Virtue << " " << fool[i].Talent;
	for (int i = 0;i < rest.size();i++)
		cout << endl << rest[i].ID << " " << rest[i].Virtue << " " << rest[i].Talent;

	system("pause");
	return 0;
}

发表于 2017-03-01 13:10:09 回复(0)
啥头像
总体思路:
        分组再排序。
        写的时候用了set来自动排序,但是费时,可以用vector先存,再用sort排序

代码如下:
#include <iostream>
#include <set>

using namespace std;

struct Person
{
    int id, virtue, talent, sumGrade;
    bool operator<(const Person& that) const {
        if(sumGrade != that.sumGrade) {
            return (sumGrade > that.sumGrade);
        } else if(virtue != that.virtue) {
            return (virtue > that.virtue);
        } else {
            return (id < that.id);
        }
    }
    Person(int _id, int _virtue, int _talent, int _sumGrade) {
        id = _id; virtue = _virtue; talent = _talent;
        sumGrade = _sumGrade;
    }
};

int main()
{
    ios::sync_with_stdio(false);
    // 读入数据
    set<Person> sages, nobleMan, foolMan, smallMan;
    int N, L, H; cin >> N >> L >> H;
    int id, virtue, talent;
    while(N--) {
        cin >> id >> virtue >> talent;
        if(virtue>=L && talent>=L) {
            if(virtue>=H && talent>=H) {
                sages.insert(Person(id, virtue, talent, virtue+talent));
            } else if(virtue>=H) {
                nobleMan.insert(Person(id, virtue, talent, virtue+talent));
            } else if(virtue >= talent) {
                foolMan.insert(Person(id, virtue, talent, virtue+talent));
            } else {
                smallMan.insert(Person(id, virtue, talent, virtue+talent));
            }
        }
    }
    cout <<(sages.size()+nobleMan.size()+foolMan.size()+smallMan.size()) << endl;
    set<Person>::iterator iter;
    if(sages.size()) {
        for(iter=sages.begin(); iter!=sages.end(); iter++) {
            cout << (*iter).id << " " << (*iter).virtue << " " << (*iter).talent << endl;
        }
    }
    if(nobleMan.size()) {
        for(iter=nobleMan.begin(); iter!=nobleMan.end(); iter++) {
            cout << (*iter).id << " " << (*iter).virtue << " " << (*iter).talent << endl;
        }
    }
    if(foolMan.size()) {
        for(iter=foolMan.begin(); iter!=foolMan.end(); iter++) {
            cout << (*iter).id << " " << (*iter).virtue << " " << (*iter).talent << endl;
        }
    }
    if(smallMan.size()) {
        for(iter=smallMan.begin(); iter!=smallMan.end(); iter++) {
            cout << (*iter).id << " " << (*iter).virtue << " " << (*iter).talent << endl;
        }
    }
    return 0;
} 


发表于 2015-12-25 20:50:54 回复(0)