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;
}

HDOJ-1029(简单dp或者排序)的更多相关文章

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

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

  2. HDOJ 1501 Zipper 【简单DP】

    HDOJ 1501 Zipper [简单DP] Problem Description Given three strings, you are to determine whether the th ...

  3. Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)

    D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. 『简单dp测试题解』

    这一次组织了一场\(dp\)的专项考试,出了好几道经典的简单\(dp\)套路题,特开一篇博客写一下题解. Tower(双向dp) Description 信大家都写过数字三角形问题,题目很简单求最大化 ...

  5. POJ1088:滑雪(简单dp)

    题目链接:  http://poj.org/problem?id=1088 题目要求: 一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小.求可以滑落的最长长度. 题目解析: 首先要先排一 ...

  6. Kattis - bank 【简单DP】

    Kattis - bank [简单DP] Description Oliver is a manager of a bank near KTH and wants to close soon. The ...

  7. URAL 1203 Scientific Conference 简单dp 难度:0

    http://acm.timus.ru/problem.aspx?space=1&num=1203 按照结束时间为主,开始时间为辅排序,那么对于任意结束时间t,在此之前结束的任务都已经被处理, ...

  8. hdu1160简单dp最长下降子序列

    /* 简单dp,要记录顺序 解:先排序,然后是一个最长下降子序列 ,中间需记录顺序 dp[i]=Max(dp[i],dp[j]+1); */ #include<stdio.h> #incl ...

  9. 4.15 每周作业 —— 简单DP

    免费馅饼 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissi ...

随机推荐

  1. Python 闭包及装饰器

    闭包是指延伸了作用域的函数. 自由变量(free variable) 指未在本地作用域中绑定的变量 函数装饰器用于在源码中标记函数, 以某种方式增强函数的行为. 装饰器实质,把被装饰的函数替换为新函数 ...

  2. k8s二进制部署 - dashboard安装

    配置资源清单rbac.yaml apiVersion: v1 kind: ServiceAccount metadata: labels: k8s-app: kubernetes-dashboard ...

  3. LINUX - mmap()

    内存映射函数 https://blog.csdn.net/qq_33611327/article/details/81738195

  4. springboot(五)Scheduling demo

    在项目开发过程中,经常会使用到定时任务(跑批),springboot默认已经实现了,只需要添加相应的注解就可以实现 在启动类上加入注解,开启定时任务 @SpringBootApplication @E ...

  5. Javascript实现"点按钮出随机背景色的"三个DIV

    <!DOCTYPE html> <html> <head> <title>Random_Color-Transformation</title&g ...

  6. vue 二级子路由跳转不了 bug

    vue 二级子路由跳转不了 bug @click.prevent 阻止原生事件的冒泡 <li class="tools-hover-box-list-item" v-for= ...

  7. js Array.from & Array.of All In One

    js Array.from & Array.of All In One 数组生成器 Array.from The Array.from() static method creates a ne ...

  8. git & github & git clone & 'git clone' failed with status 128

    git & github & git clone & 'git clone' failed with status 128 'git clone' failed with st ...

  9. PAUL ADAMS ARCHITECT :阿联酋和美国富人推动英国高端房地产市场

    来自2020年前三季度的数据显示,在英国高端市场上,由国际买家担保的抵押贷款交易数量最多,阿联酋目前处于领先地位.到目前为止,在2020年完成的所有交易中,有35%来自阿联酋. PAUL ADAMS ...

  10. 多种转弯角度的PBN旁切转弯图例分析

    无论世界怎样变化,我们依然是有点阳光就灿烂.面对世界的未知,最好的状态是勇敢的去面对,努力的去生活. 今天我们继续来聊一下PBN旁切转弯. PBN转弯保护区的结构通常都与它们的转弯角度大小有关,转弯角 ...