kuangbin 专题

这题,有很多种解法。

第一种:

直接比较每个数出现次数。

 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define clc(a,b) memset(a,b,sizeof(a))
const int maxn=;
int a[maxn];
int b[maxn];
int main()
{
int n; while(~scanf("%d",&n))
{
clc(b,);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
b[a[i]]++;
}
int maxx=-;
int ans;
for(int i=;i<;i++)
{
if(maxx<b[i])
{
maxx=b[i];
ans=i;
}
}
printf("%d\n",ans);
}
return ;
}

第二种:

应为输出至少(n+1)/2次的数,所以,最后result一定是要求的数。

 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define clc(a,b) memset(a,b,sizeof(a))
const int maxn=;
int a[maxn];
int b[maxn];
int main()
{
int n,i;
int t;
int cnt;
int result;
while(scanf("%d",&n)!=EOF)
{
cnt=;
for(i=; i<n; i++)
{
scanf("%d",&t);
if(cnt==)
{
cnt=;
result=t;
}
else
{
if(t==result)cnt++;
else cnt--;
}
}
printf("%d\n",result);
}
return ;
}

HDU 1029 Ignatius and the Princess IV DP的更多相关文章

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

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

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

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

  4. hdu 1029 Ignatius ans the Princess IV

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

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

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

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

  7. HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)

    此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...

  8. HDU 1029 Ignatius and the Princess IV

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

  9. HDU 1029 Ignatius and the Princess IV(数论)

    #include <bits/stdc++.h> using namespace std; int main(){ int n; while(~scanf("%d",& ...

随机推荐

  1. 通过注册表查找oracle_home的位置

    运行regedit进入注册表 ctrl+f查找oracle_home

  2. Java在mysql插入数据的时候的乱码问题解决

    今天在使用hibernate的时候,插入mysql的数据中的中文总是显示乱码,之前出现过类似的问题,但是没有太在意,今天又发生了.所以向彻底的解决一下. 参考的博文: http://www.cnblo ...

  3. HDU4548+素数

    简单题. /* */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<alg ...

  4. c++ 基础学习: 左值 概念cocos2d-x3.0的实际应用

    左值:概念baidu 1.2.6.2 与Cocos2d-x内存管理的结合 在2.x的使用场景中,CCArray和CCDictionary通常被分配在堆上,我们不得不需要考虑在适当的地方释放其内存.新的 ...

  5. [mock]12月11日

    给出一个二叉搜索树的先序遍历,要求重新构造出这个二叉搜索树.一开始给出了一个递归的算法,指出复杂度有更优的解法.然后想出来一个O(n)的算法,就是使用一个栈,然后依次判断新的节点比原来的大还是小,然后 ...

  6. 在windows下使用git需要反复输入用户名和密码的问题

    节选自我还在写的git文档中的一部分,用md写的,博客园竟然还不支持markdown,完全没有格式啊,懒得弄了,不过解决方法是没有问题的 在win下使用git,如果没有任何设置,一定会反复输入用户名和 ...

  7. Oracle Exception 处理

    1.问题来源Oracle中可以用dbms_output.put_line来打印提示信息,但是很容易缓冲区就溢出了.可以用DBMS_OUTPUT.ENABLE(1000000);来设置缓冲区的大小.但是 ...

  8. zookeeper入门必读

    (如果感觉有帮助,请帮忙点推荐,添加关注,谢谢!你的支持是我不断更新文章的动力.本博客会逐步推出一系列的关于大型网站架构.分布式应用.设计模式.架构模式等方面的系列文章) 今天我想谈谈zookeepe ...

  9. WinForm实现简单的拖拽文件到出题的功能(C#)(3)

    string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); textBox1. ...

  10. Wince6 RIL层移植

    RIL移植: 因为不同的模组,支持的AT命令有所不同,或是格式不一样,还有就是返回不一样,我们有必要对不同的模组进行RIL驱动移植. 在Response.cpp,Msg.cpp等 文件中ParseXX ...