当前位置:网站首页>*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;//计数值不一样 谁大谁排在前面
}
边栏推荐
- activemq--可持久化机制之JDBC代码
- Go基础4
- ActiveMQ -- JDBC code of persistent mechanism
- MySQL排序
- [WSN communication] optimize HWSN energy-saving clustering protocol based on MATLAB biogeography [including Matlab source code, 1989]
- Notes on in-depth analysis of C language 1
- [common tools] obtain system status information based on psutil and gputil
- Two Sum
- Publish Yum private server using nexus3 (offline intranet)
- Go基础3
猜你喜欢
随机推荐
实现简单的RESTful API服务器
The interviewer asked: how to prevent oversold? There are several ways to achieve it
activemq--可持久化机制之JDBC代码
有误差的字符串型时间比较方法String.compareTo
将list集合的某一字段拼接单个String
Idea hot deployment
Read and write mongodb database files
activemq--异步投递
matplotlib数据可视化三分钟入门,半小时入魔?
C language and SQL Server database technology
sqli-labs Basic Challenges Less1-10
初始Flask以及简单地上手应用
Notes on in-depth analysis of C language 1
Activemq-- asynchronous delivery
什么是单机、集群与分布式?
Numpy - 数组array的构造
Uniapp intercepts route jumps through addinterceptor to control whether the page needs to log in
ActiveMQ -- kahadb of persistent mechanism
数据库操作语言(DML)
C#语言和SQL Server数据库技术






![[GYCTF2020]Node Game](/img/8d/7e6c2fb2a0359298fbcc1cd8544710.png)


