zoukankan      html  css  js  c++  java
  • 出现次数最多的数 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


  • 相关阅读:
    Spring Cloud Config 配置中心
    Spring Cloud Zuul 路由网关
    Spring Cloud Hystrix 断路器
    Spring Cloud feign 服务消费者
    Spring Cloud Ribbon 负载均衡
    Spring Cloud Eureka 服务注册与发现
    CSS编辑工具
    CSS简史
    CSS简介
    Less的内置函数
  • 原文地址:https://www.cnblogs.com/bao-ZhangJiao/p/14268804.html
Copyright © 2011-2022 走看看