HDU 1004 Let the Balloon Rise(AC代码)
#include <stdio.h>
#include <string.h>
char a[][];
int b[]={};
int main()
{
int n,i,j,k,max,loc;
while(scanf("%d",&n)!=EOF&&n!=){
max=-;
k=;
loc=;
for(i=;i<n;i++){
scanf("%s",a[k]);
for(j=;j<k;j++){
if(strcmp(a[j],a[k])==){
b[j]++;
k--;
break;
}
}
k++;
}
for(i=;i<k;i++){
if(b[i]>max){
max=b[i];
loc=i;
}
b[i]=;
}
puts(a[loc]);
}
return ;
}
gets遇到回车才结束,并把回车符改为'\0'再存到字符串。注:如果输入的数字过长,这个函数会出问题,因为他不判断输入的长度的。
puts只要遇到第一个'\0'就会输出,并自动输出一个换行符。
格式:这个格式没很大问题,遇到0就结束,而不用输出换行再结束。
思路:
1、题中明确说答案只会有一个,也就是不会出现比如:2 red green的情况。
2、一个二维的字符数组,保存输入的颜色。
3、一个int型数组记录每个颜色出现的次数。(我把他初始化为0,是因为次数不重要,重要的是谁是最多的,实际上应该是该数字+1)
技巧:
1、每输入一个颜色,就对比前面输入的颜色中有没有出现过,若有,在该颜色位置对应的int数组上+1。
2、若输入的已经在之前的颜色中存在,在1中已经记录过出现的次数,那么颜色数组中刚输入的位置就可以重复利用了。
3、若输入的没有在之前的颜色中存在(即新颜色),该位置就不能被覆盖了。
4、输完之后,只需要对比一下int数组中的前k个就行啦(看代码),不用扫整个int数组了,这对于“大部分都是重复出现的颜色”情况就好多了,要是只有两个颜色,一次对比就搞定。
HDU 1004 Let the Balloon Rise(AC代码)的更多相关文章
- hdu 1004 Let the Balloon Rise(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...
- HDU 1004 Let the Balloon Rise(map的使用)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...
- HDU 1004 Let the Balloon Rise【STL<map>】
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- hdu 1004 Let the Balloon Rise
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- HDU 1004 Let the Balloon Rise map
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- hdu 1004 Let the Balloon Rise strcmp、map、trie树
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- hdu 1004 Let the Balloon Rise 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 用STL 中的 Map 写的 #include <iostream> #includ ...
- HDU 1004 Let the Balloon Rise(STL初体验之map)
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- HDU 1004 - Let the Balloon Rise(map 用法样例)
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
随机推荐
- eclipse设置总结
1.java函数折叠: windows->perferences->General->Editors->Structured Text Editors windows-> ...
- display:inline-block;如何取消标签之间的距离
<div style="font-size:0px"> <div style=" display:inline-block; zoom:1;*displ ...
- ANGULARJS 动态编译添加到dom中
在使用angularjs 时,希望通过动态构建angular模版,再通过angular进行展示. 使用 方法如下: <html ng-app="app"> <he ...
- js图片轮播图
/*焦点图*/ var Box='.carousel';//盒子 var Menu=$(Box+' .l_cursor li');//圆点菜单 var Con ...
- Html11.09CSS层叠样式表内容整理
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- LCD驱动 15-1
app: read() ---------------------------------------------------------------------------------------- ...
- Java异常--读书笔记
1. Java将异常分为两种:Checked异常和Runtime异常,Java认为Checked异常都是可以在编译阶段被处理的异常,所以强制程序处理所有的Checked异常:Runtime异常则无需处 ...
- [转] Android资源管理框架(Asset Manager)简要介绍和学习计划
转自:http://blog.csdn.net/luoshengyang/article/details/8738877 Android应用程序主要由两部分内容组成:代码和资源.资源主要就是指那些与U ...
- linux安装时出现your cpu does not support long mode的解决方法
如果你确定你的电脑支持64bit且是64bit的宿主系统,则需要修改BIOS中的Inter Virtualization Technology为enabled.
- 《Java中方法的参数传递方式只有一种:值传递》
//方法的参数传递机制(1):基本类型做形参的传递. class PrimitiveTransferTest { public static void swap(int a,int b) { //下面 ...