hdu 1029 Ignatius and the Princess IV
来源:互联网 发布:电商crm软件 编辑:程序博客网 时间:2024/06/07 03:08
acm题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=1029]
看到这道题,首先就想到用数组,用一个count数组计数,然后输出下标。但是,想一下假如测试的数字非常大,这样再用数组来存是非常占用空间的,再者题目并没用明说这个题测试当中具体最大整数的界限(比如:有N(11)个整数,那么这11个整数当中最大的整数是什么,有可能是1,也有可能是1000,因此要用多大的一个计数的数组呢?) 当然我也不知道,只是用9996这个范围大小的数组来计数倒是刚好ac了
附上ac代码
#include<stdio.h>#include<string.h>int count[9996];//这个数组用来计数 int main(){ int N,i,num; while(scanf("%d",&N)!=EOF) { memset(count,0,sizeof(count));//对数组初始化 for(i=0;i<N;i++) { scanf("%d",&num); count[num]++;//让相应的下标来实现计数 } for(i=0;;i++) { if(count[i]>=(N+1)/2) { printf("%d\n",i);//此处应输出下标 break; } } } return 0;}
(ps:我尝试了一下9986就通不过)
不可否认,用这种方式有投机取巧的“嫌疑”吧,刚好卡到了系统测试的数据那里。再者,这样也是比较浪费空间的。
换一种方法,观察一下下面的测试用例,试想一下N个数(奇)且要求至少有(N+1)/2个要找的特殊的数,假如说,现在有N=5个整数,分别是1 3 2 3 3让count=0从头开始,先是1且count变为1,然后第一个数1与第二数3比较发现不等就让count–,否则count++,以此类推,且要用flag来记录当前序列中那个特殊的数。
附上ac代码
#include<stdio.h>int main(){ int i,N,count,num,flag; while(scanf("%d",&N)!=EOF) { count=0; for(i=0;i<N;i++) { scanf("%d",&num); if(count==0) { flag=num; count=1; } else { if(flag==num) count++; else count--; } } printf("%d\n",flag); } return 0;}
在此声明,这些代码网上也都有,不可否认,当自己不会的时候就去看看别人的。雷同是不可避免,至少我的水平比较low,好多是让大神教的或者看网上的,但我仍然坚持写出来,就是希望这样可以监督自己学习,在刷这些题目时不至于某一天自己坚持不下来了。^^
阅读全文
1 0
- 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
- Ignatius and the Princess IV hdu 1029
- 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
- hdu - 1029 -Ignatius and the Princess IV
- php+mysql数据分页显示
- Leetcode 376. Wiggle Subsequence 解题报告
- 五个主流以太网技术比较
- Zookeeper单机安装
- OSI七层网络模型
- hdu 1029 Ignatius and the Princess IV
- 2017-12-24
- 使用Ajax实现对验证码验证提示(struts+jsp+Ajax)
- Linux入门第一周心得体会!!
- Spring Framework--ApplicationComtext(2)以ClassPathXmlApplicationContext看ApplicationContext
- 【第一次机房收费系统】退卡
- Spring MVC单元测试
- TypeScript学习心得总结(一)
- 详解数组实现隐式二叉堆及堆排序