HDOJ-1029(简单dp或者排序)
2024-09-06 06:46:02
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;
}
最新文章
- 有关DOM
- 在新浪云SAE中使用smarty引擎模版
- arcgis union 0x80040218
- CSS 仿Excel表格功能
- BZOJ 1588:营业额统计(Splay)
- tilemap坐标转换
- 转自 Good morning 的几句精辟的话
- 一个react的完整项目展示
- 【KMP】Oulipo
- 转:JMeter基础--逻辑控制器Logic Controller
- 02 easyui -parser
- Python系列之 - 面向对象(1)
- Servlet中的跳转(redirect和forward)
- java 源码编译
- C# Serialize
- 使用混淆ProGuard压缩代码和资源/减少方法数量
- 最短路问题 Floyd+Dijkstra+SPFA
- 在django中怎么解决没有MySQLdb库的问题
- 乾坤合一~Linux设备驱动之块设备驱动
- websocket简单理解
热门文章
- HDU - 3374 String Problem (kmp求循环节+最大最小表示法)
- Educational Codeforces Round 89 (Rated for Div. 2) C. Palindromic Paths(贪心)
- Codeforces Round #697 (Div. 3) D. Cleaning the Phone (思维,前缀和)
- 关于贪心算法的经典问题(算法效率 or 动态规划)
- JavaScript——六
- WPF 只读集合在 XAML 中的绑定(WPF:Binding for readonly collection in xaml)
- InnoDB 存储引擎简介
- Eazfuscator.NET(.net混淆工具)
- Win10 Nodejs搭建http-server注意点
- mimikatz+procdump 提取 Windows 明文密码