(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
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?
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.
which contains the special number you have found.
1 1 1 1 1 1 1
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的更多相关文章
- 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 ...
- HDOJ.1029 Ignatius and the Princess IV(map)
Ignatius and the Princess IV 点我跳转到题面 点我一起学习STL-MAP 题意分析 给出一个奇数n,下面有n个数,找出下面数字中出现次数大于(n+1)/2的数字,并输出. ...
- kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- hdu 1029 Ignatius ans the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- HDU 1029 Ignatius and the Princess IV --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- 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 ...
- Ignatius and the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- Ignatius and the Princess IV(乱搞一发竟然过了)
B - Ignatius and the Princess IV Time Limit:1000MS Memory Limit:32767KB 64bit IO Format:%I64 ...
- [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 ...
随机推荐
- 高级程序设计JavaScript
JavaScript简介 一.因何而生: 方便 表单验证操作(输入验证器) 1995年 2月 Netscape 布兰登 艾奇 JavaScript是一种专门为与网页交互而设计的脚本语言. 二.实现组成 ...
- 09-java学习-数组-冒泡排序-选择排序-数组工具类编写-查找-扩容
数组的排序算法 查找算法 数组协助类Arrays的学习和使用 数组的扩容
- 运用PDO存储将图片、音频文件存入数据库
在数据库中创建表格的时候,有一个字段为image,用来保存图片,那么其类型就是blob,关于blob,百度百科是这样描述的 BLOB (binary large object),二进制大对象,是一个可 ...
- Tomcat7解决java.lang.OutOfMemoryError: PermGen space
上述两参数,可根据实际情况,逐渐调大.
- Activiti 用户手册
https://tkjohn.github.io/activiti-userguide/
- Setting Tomcat Heap Size (JVM Heap) in Eclipse
this article picked from:http://viralpatel.net/blogs/setting-tomcat-heap-size-jvm-heap-eclipse/ Rece ...
- JavaScript封装方法,兼容参数类型为Number和String
/** * 依据Kind确定跳转到目标列表页面. * @param kind */ function gobackByKind(kind) { var kindStr = String(kind); ...
- Linux下数据库备份恢复过程
1. 远程进入Linux服务器. 2. 一般登录的是root用户, 第一步切换到Oracle用户, 命令: su - oracle 3. 查看服务器上面数据库的监听的状况 lsnrctl 之后输入命令 ...
- oracle 18c centos7 设置开机自动启动Oracle
学习自: http://blog.csdn.net/condywl/article/details/57129696 1. 在root用户下进行修改 配置文件 /etc/oratab vim /etc ...
- 配置dataimport时候 如果css样式有问题 要修改index和admin的版本号