(Arrays.sort() 或 map) Ignatius and the Princess IV hdu1029

Ignatius and the Princess IV

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

借鉴链接:https://blog.csdn.net/tigerisland45/article/details/52146154

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

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

Author

Ignatius.L

这个题的题意就是:

输入n(n是奇数),然后输入n个整数,求其中至少出现(n+1)/2次的整数(至少有(n+1)/2个整数)。

思路:

n是奇数,(n+1)/2是n的一半以上,只要将n个数据排序,出现(n+1)/2次的整数必然会出现在中间位置。

JAVA代码:

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner inScanner = new Scanner(System.in);
        while(inScanner.hasNext()) {
            int n = inScanner.nextInt();
            int[] x = new int[n];  //要掌握java的数组初始化的用法。
            for(int i = 0;i<n;i++) {
                x[i] = inScanner.nextInt();
            }
            Arrays.sort(x); //注意sort的用法,记住。
            System.out.println(x[(n+1)/2]);
        }
    }

}

C++代码:(用了map(),思路有点复杂)

#include <iostream>
#include <map>
using namespace std;
int main()
{
    int n;
    map<int,int> a;
    while(cin>>n)
    {
        a.clear();
        int m;
        int b=n;
        while(b--)
        {
            cin>>m;
            a[m]++;
        }
        for(map<int,int>::iterator it=a.begin();it!=a.end();it++)
        {
            if(it->second>=(n+1)/2)
            {
                cout<<it->first<<endl;
                break;
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Weixu-Liu/p/9655255.html

时间: 2024-08-01 06:09:35

(Arrays.sort() 或 map) Ignatius and the Princess IV hdu1029的相关文章

HDU 1029: Ignatius and the Princess IV

Ignatius and the Princess IV ///@author Sycamore ///@date 8/8/2017 #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; while (cin >> n) { map<int, int>v; int t,ans=-1; for (int i = 1;

HDU1029 Ignatius and the Princess IV

问题链接:HDU1029 Ignatius and the Princess IV.基础练习题,用C++语言编写. 题意简述:输入n(n是奇数),然后输入n个整数,求出现(n+1)/2次的整数. 问题分析:n是奇数,(n+1)/2是n的一半以上,只要将n个数据排序,出现(n+1)/2次的整数必然会出现在中间位置. 本问题使用C++语言编写的原因是函数sort()的参数简单,使用方便. AC的C++语言程序如下: /* HDU1029 Ignatius and the Princess IV */

[2016-03-27][HDU][1029][Ignatius and the Princess IV]

时间:2016-03-30 22:03:01 星期三 题目编号:[2016-03-27][HDU][1029][Ignatius and the Princess IV] 题目大意:给定n个数字,输出其中出现次数超过n/2的数字 #include <algorithm> #include <cstdio> using namespace std; const int maxn = 1E6 + 10; int a[maxn]; int main(){ int n; while(~sc

杭电 1029 Ignatius and the Princess IV

http://acm.hdu.edu.cn/showproblem.php?pid=1029 Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others) Total Submission(s): 16754    Accepted Submission(s): 6730 Problem Description "OK, you ar

HDU 1029 Ignatius and the Princess IV --- 水题

HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出现的次数, 若次数一旦达到(n+1)/2,即输出a[i] 注意能出现(n+1)/2次数的最多只有一个 /* HDU 1029 *Ignatius and the Princess IV --- dp */ #include <cstdio> #include <cstring> #in

Ignatius and the Princess IV 水

B - Ignatius and the Princess IV Time Limit:1000MS     Memory Limit:32767KB     64bit IO Format:%I64d & %I64u Submit Status Description "OK, you are not too bad, em... But you can never pass the next test." feng5166 says. "I will tell y

[kuangbin带你飞]专题十二 基础DP1 B - Ignatius and the Princess IV

B - Ignatius and the Princess IV 题目链接:https://vjudge.net/contest/68966#problem/B 题目: "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

Ignatius and the Princess IV

Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others)Total Submission(s): 24410    Accepted Submission(s): 10261 Problem Description "OK, you are not too bad, em... But you can never pass the

[ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)

笨蛋的难题(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述        笨蛋之所以称为笨蛋,是因为他有点路痴.他一旦不高兴,就必然一个人漫无目的的出去走走.今天下雨了,他又不高兴了,怎么办?那就出去走呗,这不又丢了,这次幸好记下出来时的方向,并且在一张纸上密密麻麻的记下了他拐的弯(他很聪明吧,拐的弯都是90度的弯),0代表左拐,1代表右拐,那么多0.1,他实在看不下去了,正好遇见善良加聪明的你,你能告诉他,他现在面向哪吗? 输入 多组测试数据 第一行 输入: