ccf:2013-12-01出现次数最多的数(应试技巧 +解题思路 + 满分代码)

应试技巧

  1. 代码开头写上下面三个头文件(包含了一般要使用的文件)
#include <iostream>
#include <cstring>
#incldue <algorithm>
  1. 数组比边界范围多开十个单位(防止越界)

  2. 如果输入的数据小于十万用cin 或 scanf 均可,大于十万用scanf(输入速度更快)

  3. 时间限制1~2s,表示c++代码总操作次数为一千万到一亿为最佳,所以想出一种思路时,可以自己预估一下时间复杂度是否会超时

  4. 时间和空间允许的条件下,写出最快能写出来并且AC的代码(不求最优算法,不求代码过分精简–容易出bug)

题目

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QMDbI0Cq-1635424924031)

解题思路

数组模拟哈希表(用于记录每个数字出现的个数),遍历一遍哈希表找出现自处最多且值最小的数

时间复杂度预估:1000 + 10000(1000为读入数组 ,10000为遍历数组)

代码实现

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int N = 10010;

int a[N]; //用数组模拟哈希表,下标为当前出现的数,数组内部记录该数出现的次数
int n;

int main()
{
    cin >> n;
    
    for (int i = 0; i < n; i ++)
    {
        int x;
        cin >> x;
        a[x] ++;
    }
    
    int res = 0; //记录出现最大那个数
    
    for (int i = 0; i < N; i ++)
    {
        if(a[i] > a[res]) res = i;
    }
    
    cout << res;
    return 0;
}

Supongo que te gusta

Origin blog.csdn.net/qq_51800570/article/details/121022825
Recomendado
Clasificación