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. Java 修饰符顺序问题

    What is a reasonable order of Java modifiers (abstract, final, public, static, etc.)? http://stackov ...

  2. Red Hat 5.8 CentOS 6.5 共用 输入法

    pick up from http://jingyan.baidu.com/article/20b68a885a3607796cec622c.html

  3. C++ cout执行顺序

    C++ cout执行顺序 问题描述是这样的:如果在cout中调用函数,同时这个函数中包含输出语句,那么会先输出哪一句? 仔细一看,突然发现对C++的内容遗忘了,确实一下子看不出来输出的先后问题. 实现 ...

  4. MySql连接空闲8小时自动断开引起的问题

    一.问题描述 ​ 最近遇到了一个奇怪的MySql数据库问题,好几次前一天晚上历史数据还正常存储,第二天早上来了看实时数据存储还正常,历史数据不存储了.找了好久也没找到问题.后来仔细想了想,历史数据设置 ...

  5. PHP hexdec() 函数

    hexdec() 函数把十六进制转换为十进制. 语法 hexdec(hex_string) 参数 描述 hex_string 必需.规定要转换的十六进制数. 说明 返回与 hex_string 参数所 ...

  6. python slots

    正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: >>> class Studen ...

  7. linux运维掌握不熟练命令用法记录

    rename   :批量修改文件名 [root@Dannyserver test]# ls .txt .txt .txt .txt [root@Dannyserver test]# rename 't ...

  8. 使用pygal_maps_world展示世界地图

    pygal.i18n在2.0版本以后改为pygal_maps_world.i18n获取国家码和国家名对应关系下载安装包:pygal_maps_world-1.0.2.tar.gz解压后命令行安装: p ...

  9. python之attrgetter函数对对象排序

    # 使用attrgetter函数对对象排序 # attrgetter处理对象,itemgetter处理序列 from operator import attrgetter class user(): ...

  10. ceph 安装过程

    安装依赖: yum install -y yum-utils && yum-config-manager --add-repo https://dl.fedoraproject.org ...