当前位置:网站首页>*6-1 CCF 2015-03-2 numerical sorting
*6-1 CCF 2015-03-2 numerical sorting
2022-07-25 09:39:00 【Ye Xiaobai】
Number order
Title Description

Source code
#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;// Numbers
int count;// frequency
}num[N + 1];
bool cmp(number a, number b)
{
if (a.count == b.count)
{
return a.num < b.num; // If the count value is the same, who is the youngest and who is in the front
}
return a.count > b.count;// Different count values Who is big and who is in front
}
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++)// according to rnum Subscript of and the number of Statistics initialization num Array
{
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;
}
About this problem
The difficulty of this problem is actually that after recording How to sort
It's used here sort Sort By default, it is in ascending order Here Define a sort method for him cmp This is the key to solving the problem
sort(num,num+ncount,cmp);
bool cmp(number a, number b)
{
if (a.count == b.count)
{
return a.num < b.num; // If the count value is the same, who is the youngest and who is in the front
}
return a.count > b.count;// Different count values Who is big and who is in front
}
边栏推荐
猜你喜欢

How to convert object data into arrays

对象数据如何转化成数组

Student management system (summary)

【cf】Round 128 C. Binary String

Android & Kotlin : 困惑解答

Object initialization

OC -- Foundation -- Collection

Browser access to swagger failed with error err_ UNSAFE_ PORT

如何将其他PHP版本添加到MAMP

TCP network application development process
随机推荐
STM32+HC05串口蓝牙设计简易的蓝牙音箱
变量名可以用中文?直接把人干蒙了
Machine learning -- detailed introduction of standardscaler (), transform (), fit () in sklearn package
Browser access to swagger failed with error err_ UNSAFE_ PORT
~1 ccf 2022-06-2 寻宝!大冒险!
Data query language (DQL)
关于学生管理系统(注册,登录,学生端)
@3-1 CCF 2020-09-1 称检测点查询
Kotlin协程:协程的基础与使用
Flutter Rive 多状态例子
【代码源】每日一题 国家铁路
Database operation language (DML)
OC--Foundation--字符串+日期和时间
Numpy - Construction of array
Voice chat app source code - produced by NASS network source code
Redis set structure command
作业7.19 顺序表
*7-1 CCF 2015-09-1 数列分段
[code source] National Railway
【代码源】每日一题 分数拆分