此题无法用JavaAC,不相信的可以去HD1029题试下!

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

题意:就是在一行数中找出那个出现次数大于等于(n+1)/2的那个数,题目保证那个数只有一个!

此题有个坑,用Java无法AC,无论用桶排序,快排,还是DP都无法AC。会超时!

简单题,就不分析了。此处把Java代码也写上了。

AC的c语言代码:(后面有Java的(3种方法都用了))

#include <stdio.h>
#include <stdlib.h> int main()
{
int n;
while(~scanf("%d",&n)){
int i;
int con=0;
int m;
int t;
for(i=0;i<n;i++){
scanf("%d",&t);
if(con==0){
m=t;
con++;
}else{
if(t==m){
con++;
}else{
con--;
} } }
printf("%d\n",m); } return 0;
}

Java的超时代码:3种方法!

package cn.hncu.acm;

import java.util.Arrays;
import java.util.Scanner; /**
* @author 陈浩翔
* @version 1.0 2016-6-18
*/
//总结:此题无法用Java在2000ms内AC。 测试数据过多,输入流需要耗费太多时间。 public class P1029 {
/*
//第一种方法 排序后判断
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n =sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
Arrays.sort(a);
System.out.println(a[(n+1)/2]);
}
}
//超时
*/ /*
//第二种方法 DP
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
int con=0;
int result=0;
for(int i=0;i<n;i++){
int m = sc.nextInt();
if(con==0){
result=m;
con++;
}else{
if(m==result){
con++;
}else{
con--;
}
}
}
System.out.println(result);
}
}
超时
*/ /*
//桶排序
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
int a[] = new int[500000];
int m=0;
int t=0;
for(int i=0;i<n;i++){
t=sc.nextInt();
a[t]++;
if(a[t]>=(n+1)/2){
m=t;
}
}
System.out.println(m);
}
}
超时
*/
}

HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)的更多相关文章

  1. Ignatius and the Princess IV (简单DP,排序)

    方法一:    直接进行排序,输出第(n+1)/2位置上的数即可. (容易超时,关闭同步后勉强卡过) #include<iostream> #include<cstdio> # ...

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

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

  3. 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 ...

  4. 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 ...

  5. hdu 1029 Ignatius ans the Princess IV

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

  6. [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 ...

  7. HDU 1029 Ignatius and the Princess IV (动态规划、思维)

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

  8. hdu 1028 Ignatius and the Princess III 简单dp

    题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...

  9. HDU 1029 Ignatius and the Princess IV

    解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...

随机推荐

  1. Entity SQL 初入

    Entity SQL 是 ADO.NET 实体框架 提供的 SQL 类语言,用于支持 实体数据模型 (EDM).Entity SQL 可用于对象查询和使用 EntityClient 提供程序执行的查询 ...

  2. CentOS安装+配置+远程

    这篇博客我之前写在了csdn,转了过来,这篇是自己认为写的比较有技术含量的文章^_^ 最近和CentOS打了交到,其中遇到了很多问题,于是看了一些博客,解决了一些问题,但是都不是特别全面,所以想来一篇 ...

  3. 【CMD】findstr命令

    findstr用来搜索匹配字符串的文件. FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file] [ ...

  4. 易买网(注册Ajax讲解)

    关于注册(用到Ajax) 运用onblur进行时时刷新 创建所需用的Servlet 好了 Ajax其实不是很难  如果还是不懂可以私信我呦-^^-!

  5. 网站开发常用jQuery插件总结(11)折叠插件Akordeon

    实现折叠菜单,可以完全不使用插件.如果使用jquery的话,实现起来也比较简单,我们只需要定义折叠菜单的样式,然后使用jquery中的渐隐渐现就可以了.如果我们自己写折叠菜单,可以方便的使用我们自己的 ...

  6. C#基础(一)——C#中反斜杠/n与/r的区别

    最近在公司实习的过程中,遇到了字符串换行的问题,百度了一下,发现字符串换行的问题还挺多,总结一下最基本的点,以防忘记. \n—>换行符(New Line),作用为换行符后面的字符串显示到“下一行 ...

  7. HTML5列表

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. php快递单号查询源码

    贴下记录下php查询快递单号的源码,能查询各种快递的快递单号记录,中通.圆通快递.顺丰快递等都不是问题.只需要在 爱快递(www.aikuaidi.cn)上面申请一个快递key即可,下面把源码分享下, ...

  9. PowerDesigner 如何添加每个表中共用的字段及自动添加注释

    PowerDesigner 如何添加每个表中共用的字段: 有时候在创建表的时候会有一些共用的字段,但是每一张表都要去创建,这样做很麻烦,特别是这样重复的工作,稍不留意就会出现问题,实际上在PD中有这样 ...

  10. 几MB的大图片变成几百KB

    使用windows自带的“画图”工具就可以. 1.用“画图”打开图片. 2.点击“重新调整大小” 弹出如下窗口 修改这里的“水平”和“垂直”,如都从100改为30.改完之后,点击确定,最后再“保存”或 ...