杭电1029 Ignatius and the Princess IV
来源:互联网 发布:sysctl 优化 编辑:程序博客网 时间:2024/05/22 03:44
Ignatius and the Princess IV
题目信息:
坏人对英雄说:我告诉你一个奇数n,然后n个数字,你必须找出其中出现次数大于(n+1)/2的数,否则我就把公主给杀掉,哈哈。
现在你昨晚英雄要解救公主;
输入:
多组输入数据,每组包括两行,第一行是一个奇数n;第二行是n个数字;
输出:
每组输入数据输出一行,为 你找出的那个数
//此方法必须要求存在一测试数据出现次数大于(n+1)/2; #include<stdio.h>int main(){int n,m,i,count,max;while(~scanf("%d",&n)){count=0;while(n--){scanf("%d",&m);if(count==0){max=m;count++;}else {if(max==m)count++;else count--;}}printf("%d\n",max);}return 0;}
//这个方法也要求输入数据中存在一数出现次数大于(n+1)/2; #include<stdio.h>#include<algorithm>using namespace std;int a[1000000];int main(){int n,i,j;while(scanf("%d",&n)!=EOF){for(i=0;i<n;++i)scanf("%d",&a[i]);sort(a,a+n);j=n/2;printf("%d\n",a[j]);}return 0;}
//超时,但可以输出出现次数最多的数据,而对//测试数据出现次数无要求 #include<stdio.h>#include<string.h>int a[1000000]={0};int main(){int n,b[1001];while(scanf("%d",&n)!=EOF){int m=-1,max=-1,k=0;memset(a,0,sizeof(a));for(i=0;i<n;++i){scanf("%d",&b[i]);a[b[i]]++;if(m<b[i])m=b[i];}for(i=0;i<=m;++i){if(a[i]>max){max=a[i];k=i;}}printf("%d\n",k);}return 0;}
0 0
- 杭电OJ 1029:Ignatius and the Princess IV
- 杭电 1029 Ignatius and the Princess IV
- 杭电 HDU 1029 Ignatius and the Princess IV
- c语言杭电1029Ignatius and the Princess IV
- 杭电1029 Ignatius and the Princess IV
- 杭电HDOJ 1029 Ignatius and the Princess IV
- 杭电1029 之 Ignatius and the Princess IV
- 杭电 1029 Ignatius and the Princess IV (技巧数学题)
- 杭电OJ(HDOJ)1029题:Ignatius and the Princess IV(数论)
- 杭电ACM OJ 1029 Ignatius and the Princess IV 快速排序 挖坑填埋法
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- LeetCode之Remove Duplicates from Sorted Array
- PostgreSQL RULE
- 滚动条的自绘
- 输出乘法口诀表
- iOS真机调试
- 杭电1029 Ignatius and the Princess IV
- 正则提取<div>中间的内容
- Jquery求table的各个的td之和
- 关于flashback
- iOS学习之sqlite的创建数据库,表,插入查看数据
- BlueMix与商业智能BI(智慧医疗场景)
- poj 1239 dp(拆分成Increasing Sequences )
- 关于结构体对齐的问题
- shell中while+for的使用方法