zoukankan      html  css  js  c++  java
  • hdu-1029 Ignatius and the Princess IV

    题目链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=1029

    题目类型:

    给一个奇数个列的数组,其中一定存在某个数字,该数字的个数是大于一半的,问这个数字是几

    解题思路:

    1、sort一遍,直接输出下标为n/2的数。

    2、将数组开为1000010,然后将数组置0,读入一个数,下标为该数的数值+1,最后进行一次循环,判断那个数值最大,输出即可。(当年还不会快排所以用的这个方法,亲测AC)。

    题目:

    Ignatius and the Princess IV

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others)
    Total Submission(s): 31704    Accepted Submission(s): 13646


    Problem Description
    "OK, you are not too bad, em... But you can never pass the next test." feng5166 says.

    "I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.

    "But what is the characteristic of the special integer?" Ignatius asks.

    "The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.

    Can you find the special integer for Ignatius?
     
    Input
    The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
     
    Output
    For each test case, you have to output only one line which contains the special number you have found.
     
    Sample Input
    5 1 3 2 3 3 11 1 1 1 1 1 5 5 5 5 5 5 7 1 1 1 1 1 1 1
     
    Sample Output
    3 5 1
    # include <stdio.h>
    # include <string.h>
    # define N 1000010
    int n,a[N],b[N];
    
    int main ()
    {
        int i,ret,max;
        while(scanf("%d",&n)!=EOF)
        {
            memset(b,0,sizeof(b));
            max=0; ret=0;
            for(i=0;i<n;i++)
            {
                scanf("%d",&a[i]);
                b[a[i]]++;
                if(b[a[i]] >max)
                {
                    max=b[a[i]];
                    ret=a[i];
                }
            }
            printf("%d
    ",ret);    
        }
        return 0;
    }
  • 相关阅读:
    随机产生16进制颜色值
    关于单元测试的思考--Asp.Net Core单元测试最佳实践
    使用xUnit为.net core程序进行单元测试
    SQLSERVER——查看阻塞信息(sp_who_lock优化无误版)
    SQLServer连接查询之Cross Apply和Outer Apply的区别及用法
    .netcore 写日志(使用NLog,log4net)
    概率与影响矩阵
    WebApi Helper帮助文档 swagger
    C# 使用HttpWebRequest Post提交数据,携带Cookie和相关参数示例
    解决哈希(HASH)冲突的主要方法
  • 原文地址:https://www.cnblogs.com/love-sherry/p/6941505.html
Copyright © 2011-2022 走看看