[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",& ...
随机推荐
- java运行时间计算
long startTime = System.currentTimeMillis(); //获取开始时间 doSomething(); //测试的代码段 long endTime = System. ...
- Mysql 数据库备份工具 xtrabackup
1.安装测试数据库mysql5.7 详细步骤 yum install -y gcc gcc-c++ cmake bison ncurses-devel .tar.gz cd mysql-/ cmake ...
- python(6)-- 模块
python模块: 定义:Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 作用:(a) 模块让你能够有逻辑地组织你 ...
- flash编程实例源代码下载
原文发布时间为:2008-08-20 -- 来源于本人的百度文章 [由搬家工具导入] http://library.sx.zj.cn/shgp/ActionScript编程实例详解.rar
- 小谈c#数据库存取图片的方式
第一种方式 文件夹与数据库配合 /// <summary> /// 上传图片 /// </summary> /// <param name="FUSShop ...
- HDU 1999 不可摸数【类似筛法求真因子和】
不可摸数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Codeforces 691E Xor-sequences(矩阵加速DP)
题目链接 Xor-sequences 利用矩阵加速. 先预处理出当序列长度为$2$的时候的方案数. 也就是说这个序列起点是$a[i]$终点是$a[j]$且中间没有任何元素. 但是所求的$k$很大,序列 ...
- win7下用PyInstaller把Python代码打包成exe文件
2013-11-05 22:02:14| 1.安装 使用PyInstaller需要安装PyWin32. 下载与Python对应的PyInstaller版本,解压后就算安装好了. 例如,安装了PyI ...
- 书写一个程序,把变量n的初始值设置为1957,然后利用除法运算和取余运算把变量n的每一位数字都抽出来并打印
class number { void num(){ int a,b,c,d; int n=1957; a=n/1000; b=n/100%10; c=n/10%10; d=n%10; System. ...
- IntelliJ IDEA关闭代码自动补全
关闭代码自动补全之后,可以使用[Ctrl]+[P]进行强制调出提示. [Editor]-[Code Completion]页里有个[Case sensitive completion],可以设置只第一 ...