• 出现次数最多的数 201312-1


    问题描述
      给定n个正整数,找出它们中出现次数最多的数。如果这样的数有多个,请输出其中最小的一个。
    输入格式
      输入的第一行只有一个正整数n(1 ≤ n ≤ 1000),表示数字的个数。
      输入的第二行有n个整数s 1, s 2, …, s n (1 ≤ s i ≤ 10000, 1 ≤ i ≤ n)。相邻的数用空格分隔。
    输出格式
      输出这n个次数中出现次数最多的数。如果这样的数有多个,输出其中最小的一个。
    样例输入
    6
    10 1 10 20 30 20
    样例输出

    10


    参考代码:

    #include <iostream>
    using namespace std;
    struct num{
    int s;
    int times =1;
    };
    int main(){
    int n;
    cin >> n;
    num *t = new num[n];
    for (int i = 0; i < n;i++)
    {
    cin >> t[i].s;
    }

    num temp ;
    for (int i = 0; i < n;i++)
    {
    for (int j = 0; j < n-1;j++)
    {
    if (t[j].s>t[j+1].s)
    {
    temp = t[j];
    t[j] = t[j + 1];
    t[j + 1] = temp;
    }
    }
    }
    int x = 1;
    for (int i = 1; i < n;i++)
    {
    if (t[i].s == t[i - 1].s){
    x++;
    t[i-1].times = 0;
    }
    else{
    t[i-1].times = x;
    x = 1;
    }
    }
    if (x!=1){
    t[n - 1].times = x;
    }
    for (int i = 0; i < n; i++)
    {
    for (int j = 0; j < n-1; j++)
    {
    if (t[j].times<t[j + 1].times)
    {
    temp = t[j];
    t[j] = t[j + 1];
    t[j + 1] = t[j];
    }
    }
    }
    int sum = 0;
    int result = t[sum].s;
    while (t[sum].times == t[sum + 1].times){
    if (result > t[sum + 1].s){
    result = t[sum+1].s;
    }
    sum++;
    }
    cout << result;
            return 0;
    }

    提交结果:

    代码长度编程语言评测结果得分时间使用空间使用
    920B C++ 正确 100 15ms 504.0KB


  • 相关阅读:
    网络编程【二】socket(套接字)初识
    网络编程【一】操作系统的发展史
    面向对象【十三】类的魔术方法
    面向对象【十二】包装和授权
    面向对象【十一】类内置的attr属性
    面向对象【十】反射
    openwrt 编译错误
    修改openwrt 终端登录欢迎界面
    openwrt quilt 使用
    openwrt luci 入门介绍
  • 原文地址:https://www.cnblogs.com/bao-ZhangJiao/p/14268804.html
Copyright © 2020-2023  润新知