当前位置:网站首页>*6-1 CCF 2015-03-2 数字排序
*6-1 CCF 2015-03-2 数字排序
2022-07-25 09:19:00 【叶萧白】
题目描述

源代码
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
int n;
const int N = 1005;
int rnum[N + 1];
struct number
{
int num;//数字
int count;//次数
}num[N + 1];
bool cmp(number a, number b)
{
if (a.count == b.count)
{
return a.num < b.num; //计数值一样谁小谁排在前面
}
return a.count > b.count;//计数值不一样 谁大谁排在前面
}
int main()
{
cin >> n;
memset(num, 0, sizeof(num));
int i, nindex;
for (i = 1; i <= n; i++)
{
cin >> nindex;
rnum[nindex]++;
}
for (int i = 1; i <= n; i++)
{
num[i].count = 0;
}
int ncount = 0;
for (i = 1; i <= N; i++)//根据rnum的下标及统计的次数 初始化num数组
{
if (rnum[i] != 0)
{
num[ncount].num = i;
num[ncount].count = rnum[i];
ncount++;
}
}
sort(num,num+ncount,cmp);
for (i = 0; i < ncount; i++)
{
cout << num[i].num << " " << num[i].count << endl;
}
return 0;
}
关于这题
这道题的难点其实在于在记录好了之后 如何排序
这里用到了sort 排序 默认情况下是升序 我们这里 给他定义了一种排序方法cmp这个为解题关键
sort(num,num+ncount,cmp);
bool cmp(number a, number b)
{
if (a.count == b.count)
{
return a.num < b.num; //计数值一样谁小谁排在前面
}
return a.count > b.count;//计数值不一样 谁大谁排在前面
}
边栏推荐
猜你喜欢

Thick willow dustpan, thin willow bucket, who hates reptile man? Asynchronous synergism, half a second to strip away a novel

What are stand-alone, cluster and distributed?

卷积神经网络的兴趣简单介绍

idea中将lib目录下的jar包加入到项目中

ActiveMQ -- JDBC code of persistent mechanism

Wechat applet obtains the data of ---- onenet and controls the on-board LED of STM32

Nacos搭建配置中心出现client error: invalid param. endpoint is blank

数据查询语言(DQL)

ActiveMQ -- JDBC Journal of persistent mechanism

yarn : 无法加载文件 yarn.ps1,因为在此系统上禁止运行脚本。
随机推荐
idea实用tips---如今将pom.xml(红色)改为pom.xml(蓝色)
[WSN communication] optimize HWSN energy-saving clustering protocol based on MATLAB biogeography [including Matlab source code, 1989]
TCP网络应用程序开发流程
[GYCTF2020]Ez_Express
activemq--可持久化机制之JDBC代码
Interviewer: tell me the difference between redis and mongodb? [easy to understand]
无法再web服务器上启动调试,web服务器未能找到请求资源
ActiveMQ -- dead letter queue
Uniapp intercepts route jumps through addinterceptor to control whether the page needs to log in
~2 ccf 2022-03-1 未初始化警告
activemq--异步投递
Numpy- array属性、改变形状函数、基本运算
Ten thousand words long, one word thoroughly! Finally, someone has made business intelligence (BI) clear
Deep understanding of static keyword
MySQL takes the query result as the data updated by update, and concatenates it after the original field data (Lej)
~4.2 ccf 2021-12-1 序列查询
Idea practical tips --- now change pom.xml (red) to pom.xml (blue)
[common tools] obtain system status information based on psutil and gputil
~5 ccf 2021-12-2 序列查询新解
Excl batch import data, background public parsing method