(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 ...
随机推荐
- #个人博客作业Week1——浏览教材后提出的六个问题及软件与软件工程的提出。
1.通常,我们阅读软件比编写软件花费的时间更多.正因为编写软件比阅读软件要容易,因此代码的可读性显得尤为重要.那么我们在写程序时应该如何避免多余的,带有误导性的注释,写出一个利于帮助别人读懂程序的注释 ...
- Linux内核分析——ELF文件格式分析
ELF文件(目标文件)格式主要三种: 1)可重定向文件:文件保存着代码和适当的数据,用来和其他的目标文件一起来创建一个可执行文件或者是一个共享目标文件.(目标文件或者静态库文件,即linux通常后缀为 ...
- 软件工程(五)UML
UML 统一建模语言,又称标准建模语言.是用来对软件密集系统进行可视化建模的一种语言.包括UML语义和UML表示法两个元素. UMl图由事物和关系组成,事物:UML模型中最基本的构成元素,是具有代表性 ...
- 传参在mybatis的sql映射文件中正确获取
1.单个参数: 非自定义对象 传参:getStuById(Integer id): 取值:#{id} 单个基本类型参数,随便取值都行:#{ok} 对象: 传参:saveStudent(Student ...
- PAT 1007 素数对猜想
https://pintia.cn/problem-sets/994805260223102976/problems/994805317546655744 让我们定义 d~n~ 为:d~n~ = p~ ...
- mybatis集成redis
系统原生集成的Ehcache, 但是监控需要(version 2.7),Ehcache Monitor http://www.ehcache.org/documentation/2.7/operati ...
- FICO模块
- WPF将数据库和GridView绑定并更改GridView模板
首先来看一下如何使用GridView,在前台的话代码如下:这里仅仅举出一个例子,GridView是作为子项嵌套在ListView中的,这里的数据源是通过绑定的方式来绑定到GridView中的. < ...
- 学习笔记特别篇之orm的跨表
models.Love.objects.filter(b__name="t1",g__nams="a1") 表示先inner join b on xx 再 in ...
- Luogu2264 树上游戏(点分治)
要统计所有路径的信息,那我们考虑点分治,每次算经过分治中心的路径的贡献.然而路径的颜色数量实在是不好统计,既然只需要求从每个点出发的所有路径的颜色数量之和,那换一种思路,改为求从每个点出发包含某种颜色 ...