1062. Talent and Virtue (25)

本文详细阐述了根据特定规则对结构体进行排序的过程,包括定义分类规则和实现排序比较函数,通过实例代码展示了如何根据圣人、君子、愚人等不同等级进行排序。

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

1.主要涉及到一个根据规则排序的问题

2.分类规则

1)圣人,sages,virtue和talent都要>=high

2)君子,nobleman,virtue>=high,talent<high,talent>=low

3)愚人,fool man,low<=talent<=virtue<high

4)愚人之后仍显示的,virtue>=low,talent>=low

3.跟结构体增加了一个level的变量,记录其所在的等级,排序比较函数如下:

bool cmp(const ManNode&a, const ManNode&b)
{
	if (a.level > b.level)
		return true;
	else if (a.level == b.level && a.total > b.total)
		return true;
	else if (a.level == b.level && a.total == b.total&& a.virtue > b.virtue)
		return true;
	else if (a.level == b.level && a.total == b.total&& a.virtue == b.virtue&& a.id < b.id)
		return true;
	else
		return false;
}



AC代码:

//#include<string>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include <iomanip>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;

struct ManNode{
	int id;//输出时要注意是8位
	int virtue, talent;
	int level, total;
	ManNode() :id(0), virtue(0), talent(0), level(0), total(0){};

};
bool cmp(const ManNode&a, const ManNode&b)
{
	if (a.level > b.level)
		return true;
	else if (a.level == b.level && a.total > b.total)
		return true;
	else if (a.level == b.level && a.total == b.total&& a.virtue > b.virtue)
		return true;
	else if (a.level == b.level && a.total == b.total&& a.virtue == b.virtue&& a.id < b.id)
		return true;
	else
		return false;
}
int main(void)
{
	int manSum, low, high;
	cin >> manSum >> low >> high;
	vector<ManNode> man(manSum);
	for (int i = 0; i < man.size(); i++)
	{
		scanf("%d %d %d", &man[i].id, &man[i].virtue, &man[i].talent);
		man[i].total = man[i].virtue + man[i].talent;
		if (man[i].virtue >= high&&man[i].talent >= high)
			man[i].level = 4;//圣人,sages
		else if (man[i].virtue >= high&&man[i].talent < high && man[i].talent >= low)
			man[i].level = 3;//君子,nobleman
		else if (man[i].virtue < high && man[i].virtue >= man[i].talent && man[i].talent>=low)
			man[i].level = 2;//愚人,fool man
		else if (man[i].virtue >= low&&man[i].talent >= low)
			man[i].level = 1;//排在愚人之后,其他的不显示

	}
	sort(man.begin(), man.end(), cmp);

	int outPut = 0;
	for (int i = 0; i < man.size(); i++)
	{//统计需要显示的人数
		if (man[i].level>=1)
			outPut++;
		else
			break;
	}
	cout << outPut << endl;
	for (int i = 0; i < outPut; i++)
	{
		printf("%08d %d %d\n", man[i].id, man[i].virtue, man[i].talent);
	}

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值