Ignatius and the Princess IV

hdoj-1029

  • 这里主要是先排序,因为要找出现了一半以上的数字,所以出现的数字一定在中间

方法一:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n;
int a[10000007];
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
cout<<a[n/2]<<endl;
}
//system("pause");
return 0;
}

方法二:简单计算出每个数字出现的次数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n;
int a[10000007];
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
//cout<<a[n/2]<<endl;
int k=0;
for(int i=0;i<n-1;i++){
if(a[i]!=a[i+1]){
//cout<<k<<endl;
if(k+1>=(n+1)/2){
cout<<a[i]<<endl;
break;
}
k=0;
}else{
k++;
if(k+1>=(n+1)/2){
cout<<a[i]<<endl;
break;
}
}
} }
//system("pause");
return 0;
}

最新文章

  1. 有关DOM
  2. 在新浪云SAE中使用smarty引擎模版
  3. arcgis union 0x80040218
  4. CSS 仿Excel表格功能
  5. BZOJ 1588:营业额统计(Splay)
  6. tilemap坐标转换
  7. 转自 Good morning 的几句精辟的话
  8. 一个react的完整项目展示
  9. 【KMP】Oulipo
  10. 转:JMeter基础--逻辑控制器Logic Controller
  11. 02 easyui -parser
  12. Python系列之 - 面向对象(1)
  13. Servlet中的跳转(redirect和forward)
  14. java 源码编译
  15. C# Serialize
  16. 使用混淆ProGuard压缩代码和资源/减少方法数量
  17. 最短路问题 Floyd+Dijkstra+SPFA
  18. 在django中怎么解决没有MySQLdb库的问题
  19. 乾坤合一~Linux设备驱动之块设备驱动
  20. websocket简单理解

热门文章

  1. HDU - 3374 String Problem (kmp求循环节+最大最小表示法)
  2. Educational Codeforces Round 89 (Rated for Div. 2) C. Palindromic Paths(贪心)
  3. Codeforces Round #697 (Div. 3) D. Cleaning the Phone (思维,前缀和)
  4. 关于贪心算法的经典问题(算法效率 or 动态规划)
  5. JavaScript——六
  6. WPF 只读集合在 XAML 中的绑定(WPF:Binding for readonly collection in xaml)
  7. InnoDB 存储引擎简介
  8. Eazfuscator.NET(.net混淆工具)
  9. Win10 Nodejs搭建http-server注意点
  10. mimikatz+procdump 提取 Windows 明文密码