【CCF】出现次数最多的数(C++小白版)

本文介绍了一种使用数组实现的高效算法,用于从一组正整数中找出出现次数最多的数,若存在多个,则输出最小的那个。该算法适用于数据规模不大于1000的情况,通过直接映射每个可能的整数值到数组的对应位置,记录并比较每个数的出现频率。

出现次数最多的数

给定n个正整数,找出它们中出现次数最多的数。如果这样的数有多个,请输出其中最小的一个。

输入格式:

输入的第一行只有一个正整数n(1 ≤ n ≤ 1000),表示数字的个数。
输入的第二行有n个整数s1, s2, …, sn (1 ≤ si ≤ 10000, 1 ≤ i ≤ n)。相邻的数用空格分隔。

输出格式:

输出这n个次数中出现次数最多的数。如果这样的数有多个,输出其中最小的一个。

样例输入:

6
10 1 10 20 30 20

样例输出:

10

#include <bits/stdc++.h>
using namespace std;
int arr[10001]={0};
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    //freopen("demo.txt","r",stdin);
    int n;
    int tmp;
    int max1=-1;
    int max2=-1;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>tmp;
        arr[tmp]++;
        if(arr[tmp]>max1)
        {
             max2=tmp;
             max1=arr[tmp];
        }
        else if(arr[tmp]==max1&&tmp<max2)
        {
            max2=tmp;
        }
    }
    cout<<max2;
}

嘎嘎,这道题其实我有点懵,本来想用map的,但是搞来搞去不太可,就想着直接用数组吧,肯定会有样例过不了,哦豁,结果直接成了!太好了玐!

CCF 201503 - 2 字排序问题要求统计给定的 $n$ 个整中每个整出现的次数,并按出现次数从多到少的顺序输出。若两个整出现的次数一样多,则先输出值较小的。以下是几种不同实现方式的 C++ 代码: ### 代码实现 1 ```cpp #include<iostream> #include<algorithm> using namespace std; int a[1005]; struct data{ int n; int c; bool operator < (const data& t) const{ if(c!=t.c) return c>t.c; else return n<t.n; } }s[1005]; int main(){ int n,temp=0; cin>>n; for(int i=0;i<n;i++){ cin>>temp; a[temp]++; } int cnt=0; for(int i=0;i<1005;i++){ if(a[i]) s[cnt++]={i,a[i]}; } sort(s,s+cnt); for(int i=0;i<cnt;i++){ cout<<s[i].n<<" "<<s[i].c<<endl; } return 0; } ``` ### 代码实现 2 ```cpp #include<iostream> #include<algorithm> using namespace std; struct node{ int value,time; }a[1010]; int cmp(node i,node j){ if(i.time==j.time) return i.value<j.value; else return i.time>j.time; } int n,m,Max; int main(){ cin>>n; for(int i=0;i<n;i++){ cin>>m; a[m].time++; a[m].value=m; if(Max<m) Max=m; } sort(a,a+Max+1,cmp); for(int i=0;i<n;i++){ if(a[i].time==0) break; cout<<a[i].value<<" "<<a[i].time<<endl; } return 0; } ``` ### 代码实现 3 ```cpp #include<iostream> #include<algorithm> #define SIZE 1000+5 using namespace std; struct Node { int id; int size; Node(){} Node(int newid) :id(newid), size(0) {} bool operator < (const Node& other) const{ if (size != other.size) return size > other.size; else return id < other.id; } }nodes[SIZE]; int main() { int n, num, i; for (i = 0; i <= SIZE; i++) nodes[i] = Node(i); cin >> n; while (n--) { cin >> num; nodes[num].size++; } sort(nodes, nodes + SIZE); i = 0; while (nodes[i].size != 0) { cout << nodes[i].id << " " << nodes[i].size << endl; i++; } return 0; } ``` ### 代码实现 4 ```cpp #include<bits/stdc++.h> using namespace std; struct node{ int num; int cnt; node(int num,int cnt):num(num),cnt(cnt){} }; bool cmp(node a,node b){ if(a.cnt == b.cnt) return a.num<b.num; return a.cnt>b.cnt; } int main() { int n; unordered_map<int,int> um; vector<node> v; scanf("%d",&n); while(n--){ int num; scanf("%d",&num); um[num]++; } for(auto m:um){ v.push_back(node(m.first,m.second)); } sort(v.begin(),v.end(),cmp); for(auto i:v){ printf("%d %d\n",i.num,i.cnt); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值