(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 ...
随机推荐
- 《Linux及安全》课程实践二
编译生成新内核 一.实践原理 Linux模块是一些可以作为独立程序来编译的函数和数据类型的集合.之所以提供模块机制,是因为Linux本身是一个单内核.单内核由于所有内容都集成在一起,效率很高,但可扩展 ...
- Daily Scrum 12.8
Member Task on 12.08 Task on 12.09 仇栋民 参与M2阶段第二次决策会议 开始Task964 : 活动评论功能雏形 康家华 开始Task982 : 完成活动界面的设计稿 ...
- mysql外键关联
主键:是唯一标识一条记录,不能有重复的,不允许为空,用来保证数据完整性 外键:是另一表的主键, 外键可以有重复的, 可以是空值,用来和其他表建立联系用的.所以说,如果谈到了外键,一定是至少涉及到两张表 ...
- Comparer Under Centos 7
Kompare 效果还行.
- Node http请求信息
//1:加载http模块 httpconst http = require("http");//2:创建http 服务器var server = http.createServer ...
- centos7黑客帝国装逼
黑客帝国既视感 搜 cmatrix 然后放到本地解压缩 ,安装 yum install ncurses-devel./configure && make && make ...
- zookeeper和kafka的安装
一.安装zookeeper yum install -y java ##安装jdk1.6版本以上 wget http://mirror.bit.edu.cn/apache/zookeeper/zook ...
- sql语句中日期相减的操作
select datediff(year, 开始日期,结束日期); --两日期间隔年select datediff(quarter, 开始日期,结束日期); --两日期间隔季select datedi ...
- js邏輯
js的邏輯對象可以用於將非邏輯對象轉換為邏輯 var a=new Boolean(); a為false的幾種情況,0,-0,null,false,“”,undefined,NAN
- Freemarker空值判断
freemarker中显示某对象使用${name}. 但如果name为null,freemarker就会报错.如果需要判断对象是否为空: <#if name??> …… </#if& ...