#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. [转载]linux下svn常用指令

    一下内容转载于:http://blog.chinaunix.net/space.php?uid=22976768&do=blog&id=1640924.这个总结的很好~ windows ...

  2. input 中的enabled与disabled属性

    <style type="text/css"> *{ padding:; margin:; list-style-type: none; box-sizing:bord ...

  3. Htmlhelper—CheckBox自动生成两个input

    前言 在之前的一篇文章中小猪分享了Htmlhelper的用法.其中有意思的一个就是Checkbox,有必要单独拿出来讲一讲. Htmlhelper—CheckBox 细心的读者一定发现了当使用类似语法 ...

  4. Android调用远程Service的参数和返回值都需要实现Parcelable接口

    import android.os.Parcel;import android.os.Parcelable; public class Person implements Parcelable{ pr ...

  5. 第一个servlet小例子

    1.sendForward.jsp <%@ page language="java" contentType="text/html; charset=UTF-8&q ...

  6. bzoj 2301 莫比乌斯反演

    对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 这里题目意思很明显 对于要求的f[n] = sig ...

  7. Quartz 2D 图形上下文栈 矩阵 裁剪

    Quartz 2D 图形上下文栈  矩阵 // // DJVIew.m // 图形上下文栈 // // Created by zjj on 15/6/30. // Copyright (c) 2015 ...

  8. Flux Demo解析

    最近学习了阮一峰老师的博文 "Flux入门教程",博文中详细介绍了Flux框架和Controller view模式,并提供了Demo,受益匪浅. 现特参考阮老师的Demo,绘制了一 ...

  9. 最大公约数——Program G

    最大公约数 Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit ...

  10. mybatis多对一关联的两种方式

    第一个种是Address找到自己的user_id,扔给User,让User自己去再查一次,即使在有缓存的前提下,每遇到一个新的user_id,就会查一次,对比hibernate的话,相当于多对一eag ...