[ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)
Ignatius and the Princess IV
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32767K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
"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
N integers. The input is terminated by the end of file.
Output
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
解题思路:
给出n个数。n为奇数。求在里面至少出现(n+1)/2 次的那个数。
方法一:
同等消除。比方数 -1 -2 3 3 3 -2 ,消除一次-1 3 -2 去掉。剩余3 3 -2 。再消除一次 剩余3,3就是所求。
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std; int main()
{
int n;
int num,cnt,r;
while(scanf("%d",&n)!=EOF)//一定得写上。EOF
{
cnt=0;
while(n--)
{
scanf("%d",&num);
if(cnt==0)
{
r=num;
cnt=1;
}
if(num==r)
cnt++;
else
cnt--; }
printf("%d\n",r);
}
return 0;
}
方法二:hash打表。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=1000005;
int hash[maxn]; int main()
{
int n,num,r;
while(scanf("%d",&n)!=EOF)
{
memset(hash,0,sizeof(hash));
for(int i=1;i<=n;i++)
{
scanf("%d",&num);
hash[num]++;
if(hash[num]>n/2)
{
r=num;//找到了也不能break掉。由于有可能还没有输入完成
}
}
printf("%d\n",r);
}
return 0;
}
[ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)的更多相关文章
- 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 ...
- 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 ...
- 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 (动态规划、思维)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)
此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...
- HDU 1029 Ignatius and the Princess IV
解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...
- HDU 1029 Ignatius and the Princess IV DP
kuangbin 专题 这题,有很多种解法. 第一种: 直接比较每个数出现次数. #include<iostream> #include<string> #include< ...
- HDU 1029 Ignatius and the Princess IV(数论)
#include <bits/stdc++.h> using namespace std; int main(){ int n; while(~scanf("%d",& ...
随机推荐
- 5-Dalvik垃圾收集机制Cocurrent GC
Dalivik垃圾回收收机制Cocurrent GC简介和学习计划 导语: 在C/C++中,开发者需要手动地管理在堆中分配的内存,但是这往往导致很多问题. 1. 内存分配之后忘记释放,造成内存泄漏. ...
- Python 读取 pkl文件
使用python 的cPickle 库中的load函数,可以读取pkl文件的内容 import cPickle as pickle fr = open('mnist.pkl') #open的参数是pk ...
- Python基础教程笔记 第一章
/ 表示整除,当导入_future_模块中的version时,/ 表示正常的的除法, 此时可用//表示整除,不论数字是整型还是浮点型,都可以用//表示整除. ** 表示幂次方 例如 2**3 ...
- Secure services with TLS ---Docker配置https
官方文档:https://docs.docker.com/ee/ucp/interlock/usage/tls/
- cf 126D - Fibonacci Decomposition
题目大意 \(t\le 10^5\)组询问 每次询问\(1\le n\le 10^{18}\) 求有多少种\(n\)的\(fibonacci\)分解 分解定义为:每个\(fib\)数最多选一个,且和为 ...
- Eclipse与MyEclipse修改注释字体颜色
修改配置路劲 Window--->Preferences--->Java--->Editor--->Syntax Coloring--->Element--->Co ...
- 用.net开发wap
原文发布时间为:2010-08-19 -- 来源于本人的百度文章 [由搬家工具导入] .NET 模板:http://download.csdn.net/source/2631001 WAP应用程序结构 ...
- .NET结束外部进程 C#结束外部进程
原文发布时间为:2011-02-15 -- 来源于本人的百度文章 [由搬家工具导入] using System;namespace ConsoleApplication2{ class Prog ...
- DataBinder.Eval值的判断
原文发布时间为:2009-04-10 -- 来源于本人的百度文章 [由搬家工具导入] 问:如何对<%# DataBinder.Eval(Container.DataItem,"Ly_R ...
- 本机开发Native Development:Invalid path for NDK (转)
打开window菜单下的preference选项.选择Android,Native Development(本地开发) 选择你的NDK安装目录.但是,这个插件目前仅支持ndk的r4和r5版本,更高版本 ...