这道题来自NEUQ
​​​想上分的鲸鱼​​​
题目描述
王者荣耀是现在比较火的手机游戏,游戏中有70个英雄可供玩家挑选,玩家可以用这70个英雄参加排位赛提升段位。而刚玩这个游戏的鲸鱼学姐想提升到白金段位,但是她靠自己单独排位很难上分,于是她求助了另外几个学长,另外的学长也答应帮她,但是他们需要趁手的英雄,另外几个学长把他们想玩的英雄的编号告诉了鲸鱼学姐,鲸鱼学姐要根据他们给出的编号挑出五个需求量最大的英雄。然而,学姐要练车,没有时间来统计,于是他找到了plyjdz,然而比较懒的plyjdz(其实就是皮卡丘啦),想把这个任务交给学弟学妹们。

输入
学长们给出了很多套不同方案。(方案不超过100套)

每套方案有不超过m个英雄的编号x,编号间以空格隔开,每套方案以0为结束的标志。(5<=m<=1000,1<=x<=70)

保证输入数据合法

保证输入至少有一套方案

输出

每行输出一套方案所求出的最大需求量,按照数字的升序输出需求量最大的五个英雄的编号。

关于升序:

就是一串变量按照由小到大(数字)、由开始到结束(事件)由A到Z(字母)的顺序排列。

样例输入
1 2 3 4 5 0
1 1 2 3 4 5 6 6 0
5 5 4 1 1 2 3 2 1 4 6 6 7 8 0
15 45 12 54 45 12 45 8 48 12 45 48 45 41 2 45 48 65 23 11 25 23 16 14 15 18 19 20 12 15 1 23 45 48 12 45 54 12 45 48 45 14 41 45 48 65 23 11 25 23 16 14 15 15 15 15 15 15 0
样例输出
1 2 3 4 5
1 2 3 4 6
1 2 4 5 6
12 15 23 45 48

这道题需要用到sort
提示:algorithm头文件中的sort可以给任意对象排序,包括内置类型和自定义类型,前提是类型定义了“<”运算符。排序之后可以用lower_bound查找大于或等于x的第一个位置。带排序/查找的元素可以放在数组里,也可以放在vector里面(前者用sort(a,a+n)后者用sort(v.begin(),v.end())的方式调用)

#include <iostream>
#include <map>
#include<algorithm>
#include <queue>

using namespace std;

struct node {
int key, count;
bool operator < (const node& n) const {
//定义排序
if (count == n.count)
return key > n.key;
else
return count < n.count;
}
};

map<int, int> m;
//函数输入
bool init()
{
int v;
bool flag = false;
while (~scanf("%d", &v) && v)
{
flag = true;
m[v]++;
}
return flag;
}

int main()
{
int maxn[5];
priority_queue<node> q;

while (init()){

node keyval;
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
keyval.key = it->first;
keyval.count = it->second;
q.push(keyval);
}
int i = 0;
while (!q.empty()) {
keyval = q.top();
q.pop();
maxn[i] = keyval.key;
i++;
if(i==5)
break;
}
//清空队列
while (!q.empty()) {
q.pop();
}

sort(maxn, maxn + 5);
for (int i = 0; i < 4; i++)
printf("%d ",maxn[i]);
printf("%d\n",maxn[4]);
//清空map
m.erase(m.begin(), m.end());
}

return 0;
}

这道题和​​ccf201503-2​​(这道是老师博客的解题)数字排序差不多,嗯么,改了一下输入和其中的一些,道理一样。get了新的函数输入和sort关于结构体的排序的方法。

这是在下的一位大佬写的。相比于我写的那个用map更加简单,所以这题直接用hero的结构体,定义一个数组,对输入的进行排序。最后输出的时候注意一下形式。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=70+2;
struct hero
{
int id;
int num;
bool operator < (const hero &a) const
{
return num>a.num?1:(num==a.num?(id<a.id):0);
}
}h[N];
bool init()
{
for(int i=0;i<N;i++)
{
h[i].id=i;
h[i].num=0;
}
int v;
bool flag=false;
while(~scanf("%d",&v)&&v)
{
flag=true;
h[v].num++;
}
return flag;
}
int main()
{
int ans[5];
while(init())
{
sort(h,h+N);
for(int i=0;i<5;i++)
ans[i]=h[i].id;
sort(ans,ans+5);
for(int i=0;i<4;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[4]);
}
return 0;
}

此处的重点应该是
会使排序更简单。

bool operator < (const node& n) const {
//定义排序
if (count == n.count)
return key > n.key;
else
return

还有一个就是函数输入