HDU 1029 Ignatius and the Princess IV
2015-08-18 16:31
344 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029
题 意:找出一组n个数中出现次数大于等于(n+1)/2的数。
思 路:
一:map计算的方法。
代码如下:#include <iostream>
using namespace std;
#include <string.h>
#include <stdio.h>
#include <climits>
#include <algorithm>
#define maxn 200005
typedef __int64 LL;
int vis[1000000];
int main()
{
int n;
while( scanf ( "%d", &n ) != EOF )
{
int f = 0;
memset( vis, 0, sizeof(vis) );
for( int i = 0; i < n; i ++ )
{
int x;
scanf ( "%d", &x );
vis[x]++;
if( vis[x] >= (n+1)/2 )
f=x;
}
printf("%d\n",f);
}
return 0;
}
二:(非原创)n是个奇数,要求一个数至少出现(n+1)/2次。用time来记录解出现的次数,出现了正确解就令time自增1,不是正确解就使time自减1。
那么,正确解对应的time一定是不小于1的。可以用一个极端的例子来说明下:输入3 3 3 3 3 3 2 1 5 6 8,开始当ans=3时,time=6,那么继续执行num!=3了,
time开始自减,但最终time=1,始终不会进入程序if(time==0){}内部执行了。利用最终的time,还可以计算出解出现的次数。公式为:(n-time)/2+time。
代码如下:#include <iostream>
using namespace std;
#include <string.h>
#include <stdio.h>
#include <climits>
#include <algorithm>
#define maxn 200005
typedef __int64 LL;
int main()
{
int n;
while( scanf ( "%d", &n ) != EOF )
{
int f, time = 0;
for( int i = 0; i < n; i ++ )
{
int x;
scanf ( "%d", &x );
if( time == 0 )
{
f = x;
time++;
}
else {
if( x == f ) time++;
else time--;
}
}
printf("%d\n",f);
}
return 0;
}
题 意:找出一组n个数中出现次数大于等于(n+1)/2的数。
思 路:
一:map计算的方法。
代码如下:#include <iostream>
using namespace std;
#include <string.h>
#include <stdio.h>
#include <climits>
#include <algorithm>
#define maxn 200005
typedef __int64 LL;
int vis[1000000];
int main()
{
int n;
while( scanf ( "%d", &n ) != EOF )
{
int f = 0;
memset( vis, 0, sizeof(vis) );
for( int i = 0; i < n; i ++ )
{
int x;
scanf ( "%d", &x );
vis[x]++;
if( vis[x] >= (n+1)/2 )
f=x;
}
printf("%d\n",f);
}
return 0;
}
二:(非原创)n是个奇数,要求一个数至少出现(n+1)/2次。用time来记录解出现的次数,出现了正确解就令time自增1,不是正确解就使time自减1。
那么,正确解对应的time一定是不小于1的。可以用一个极端的例子来说明下:输入3 3 3 3 3 3 2 1 5 6 8,开始当ans=3时,time=6,那么继续执行num!=3了,
time开始自减,但最终time=1,始终不会进入程序if(time==0){}内部执行了。利用最终的time,还可以计算出解出现的次数。公式为:(n-time)/2+time。
代码如下:#include <iostream>
using namespace std;
#include <string.h>
#include <stdio.h>
#include <climits>
#include <algorithm>
#define maxn 200005
typedef __int64 LL;
int main()
{
int n;
while( scanf ( "%d", &n ) != EOF )
{
int f, time = 0;
for( int i = 0; i < n; i ++ )
{
int x;
scanf ( "%d", &x );
if( time == 0 )
{
f = x;
time++;
}
else {
if( x == f ) time++;
else time--;
}
}
printf("%d\n",f);
}
return 0;
}
相关文章推荐
- Javascript SHA-1:Secure Hash Algorithm
- [转]可视化的数据结构和算法
- 统计文件中不小于某一长度的单词的个数(泛型算法实现)
- 使用他人的MD5编码类,修改形成密码串
- Extracting Structured Data from Web Pages
- (译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.13使用CCTexture2DMutable调换调色盘
- Java中3DES加密
- Refactoring Notes-Refactoring Methods(3)
- 图书馆管理程序~~不过貌似功能!!有空再修修
- trainging contest#2(2011成都现场赛)I BY Hyoga
- C/C++头文件包含内容概览
- 堆栈的应用(1) 平衡符号 C++实现
- 程序员编程艺术第一章、左旋转字符串
- 程序员编程艺术:第三章续、Top K算法问题的实现
- 程序员编程艺术:第四章、现场编写类似strstr/strcpy/strpbrk的函数
- 十四、第三章再续:快速选择SELECT算法的深入分析与实现
- 程序员编程艺术:第七章、求连续子数组的最大和
- 程序员编程艺术:第八章、从头至尾漫谈虚函数
- 程序员编程艺术:第九章、闲话链表追赶问题
- 程序员编程艺术:第十章、如何给10^7个数据量的磁盘文件排序