#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代码)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. hdu 1004 Let the Balloon Rise

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  5. 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 ...

  6. 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 ...

  7. hdu 1004 Let the Balloon Rise 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 用STL 中的 Map 写的 #include <iostream> #includ ...

  8. 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 ...

  9. HDU 1004 - Let the Balloon Rise(map 用法样例)

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

随机推荐

  1. BZOJ1218 [HNOI2003]激光炸弹

    题目后面写着DP就当它是DP吧.. 本来是扫描线+线段树的说,但是捏5000^2还是能过滴,于是暴力枚举正方形+所谓的DP就解决了. /******************************** ...

  2. XPATH 带命名空间数据的读取

    在XML中,很多情况下有命名空间,如果直接使用XPATH 读取是会读到空节点. 解决办法如下: InputStream is=loader.getResourceAsStream("com/ ...

  3. 学习shell之前你不得不了解的小知识

    1.!!命令                   # 表示上一条输出history |grep 23         #表示历史记录中的第23条!vim                     #上一 ...

  4. Wcf Client 异常和关闭的通用处理方法

    在项目中采用wcf通讯,客户端很多地方调用服务,需要统一的处理超时和通讯异常以及关闭连接. 1.调用尝试和异常捕获 首先,项目中添加一个通用类ServiceDelegate.cs public del ...

  5. php file_get_contents curl发送cookie,使用代理

    $auth = base64_encode('LOGIN:PASSWORD');//LOGIN:PASSWORD 这里是你的账户名及密码 $aContext = array( 'http' => ...

  6. UVa 11361 - Investigating Div-Sum Property

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  7. svn cleanup failed问题解决

    1.SVN出错 今早过来Update,报如下错误: 再次更新,svn会要求你执行clean up,但执行clean up仍会报错,说有未完的work item,还要求你执行clean up.汗,陷入死 ...

  8. ubuntu php.ini文件位置

    mc@XJ > locate php.ini/etc/php5/cli/php.ini/etc/php5/fpm/php.ini

  9. Hibernate 通过 Session 操纵对象

    Session 概述 •Session 接口是 Hibernate 向应用程序提供的操纵数据库的最主要的接口, 它提供了基本的保存, 更新, 删除和加载 Java 对象的方法. •Session 具有 ...

  10. Android TextView文字横向自动滚动(跑马灯)

    TextView实现文字滚动需要以下几个要点:   1.文字长度长于可显示范围:android:singleLine="true"   2.设置可滚到,或显示样式:android: ...