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;
}

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

  1. HDU 1029 Ignatius and the Princess IV (map的使用)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1029 Ignatius and the Princess IV Time Limit: 2000/10 ...

  2. HDOJ.1029 Ignatius and the Princess IV(map)

    Ignatius and the Princess IV 点我跳转到题面 点我一起学习STL-MAP 题意分析 给出一个奇数n,下面有n个数,找出下面数字中出现次数大于(n+1)/2的数字,并输出. ...

  3. kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  4. hdu 1029 Ignatius ans the Princess IV

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

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

    HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...

  6. HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)

    HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...

  7. Ignatius and the Princess IV

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  8. Ignatius and the Princess IV(乱搞一发竟然过了)

    B - Ignatius and the Princess IV Time Limit:1000MS     Memory Limit:32767KB     64bit IO Format:%I64 ...

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

    Ignatius and the Princess IV Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32767K (Ja ...

随机推荐

  1. KETTLE集群搭建

    KETTLE集群搭建 说明: 本文档基于kettle5.4 一.集群的原理与优缺点 1.1集群的原理 Kettle集群是由一个主carte服务器和多个从carte服务器组成的,类似于master-sl ...

  2. individual reading task ---12061183 叶露婷

    Different people deserve different tasks; Once team roles are settled, there comes along a lot of ot ...

  3. #个人博客作业week3——微软必应词典的使用

    产品的调研和评测 笔者使用的是win8的必应词典客户端. 首先打开客户端,用户界面的设计十分简洁,使用方便.但是词典主页与大多外语软件的设计相仿,例如有每日一句,每日阅读等模块,并没有令人感到新奇的地 ...

  4. 安装python包时报错

    pip install numpy  时  报错: Traceback (most recent call last):  File "d:\学习\python\python-3.6.5\l ...

  5. Docker(二十七)-Docker 清理占用的磁盘空间

    1. docker system命令 docker system df命令,类似于Linux上的df命令,用于查看Docker的磁盘使用情况: docker system dfTYPE TOTAL A ...

  6. [wiki]陶德曼调停

    陶德曼调停[编辑] 维基百科,自由的百科全书 凯申物流差点和谈 目录 1背景 2调停经过 3评价 4参见 背景[编辑] 主条目:中德合作 (1911年-1941年) 1936年11月25日,德国与日本 ...

  7. 关于supervisor 的使用以及配置

    首先我个人认为,用python实现的supervisor使用了守护进程这个概念去实现一个包裹进程的概念. 他可以帮助你的进程完成失效重启,日志记录,确保在线,关机自启动等一系列的功能. 当使用supe ...

  8. Java之枚举举例

    package enumdemo; /** * 枚举类 */ public enum MAPPER { // 实例 ELEMENT_NAME("mapper"), ATTRIBUT ...

  9. 一本通1587【例 3】Windy 数

    1587: [例 3]Windy 数 时间限制: 1000 ms         内存限制: 524288 KB 题目描述 原题来自:SCOI 2009 Windy 定义了一种 Windy 数:不含前 ...

  10. SQL语言分类DQL,DML,DDL,DCL,DTL

    SQL语言共分为五大类: 数据查询语言DQL 数据操纵语言DML 数据定义语言DDL 数据控制语言DCL 数据事物语言DTL DQL 数据查询语言DQL基本结构是由SELECT子句,FROM子句,WH ...