Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 57556    Accepted Submission(s): 21037

Problem Description
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.

 
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.

 
Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
 
Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
0
 
Sample Output
red
pink

hdu_1004_Let the Balloon Rise_201307271045.c

#include <stdio.h>
#include <string.h>
int main()
{
 char str[1010][18];
 int n;
 while(scanf("%d",&n),n)
 {
 int i,j,k,m=0,num=0;
 char str1[18],str2[18];
 for(i=0;i<n;i++)
 {
  gets(str[i]);
 }
 for(j=1;j<n;j++)
 for(i=0;i<n-j;i++)
 if(strcmp(str[i],str[i+1])>0)
 {
  strcpy(str1,str[i]);
  strcpy(str[i],str[i+1]);
  strcpy(str[i+1],str1);
 }
 strcpy(str2,str[0]);
    for(i=0;i<n;i++)
    {
     if(strcmp(str2,str[i])==0)
     num++;
     else
     {
      strcpy(str2,str[i]);
      num=1;
      if(num>m)
      {
         m=num;
        
         k=i-1;
      }
     }
    }
    if(num>m)
    {
     m=num;        
        k=i-1;
    }
    printf("%s\n",str[k]);
 }       
 return 0; 
}

//超时

//参考代码如下:

#include <stdio.h>
#include <string.h>
main(){
    int n, i, j, t, max, num[1000];
    char color[1000][16];
    while(scanf("%d", &n) != EOF){
        if(n){
            num[0]=0;
            scanf("%s", color[0]);
            for(i=1; i <n; i++){
                num[i]=0;
                scanf("%s", color[i]);
                for(j=0; j <i-1; j++)
                    if(strcmp(color[i], color[j])==0) num[i] +=1;
            }
            max=num[0];
            t=0;
            for(i=1; i <n; i++)
               if(max <num[i]) {max =num[i]; t=i;}
            printf("%s\n",color[t]);
        }
    }
}

//
 
改后代码:
#include <stdio.h>
#include <string.h>
int main()
{
 char str[1010][18]; 
 int n;
 while(scanf("%d",&n),n)
 {
  int i,j,k,max;
  int num[1010]={0};
  k=max=0;
  for(i=0;i<n;i++)
  {
   scanf("%s",str[i]);
   for(j=0;j<i;j++)
   if(strcmp(str[i],str[j])==0)
   num[i]++;   
   if(num[i]>max)
   {
    max=num[i];
    k=i;
   }
  }
  printf("%s\n",str[k]);
 }
 return 0;
}

【ACM】hdu_1004_Let the Balloon Rise的更多相关文章

  1. 【ACM】hdu_1004_Let the Balloon Rise_201308141026-2

    Let the Balloon RiseTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

  3. 【ACM】HDU1008 Elevator 新手题前后不同的代码版本

    [前言] 很久没有纯粹的写写小代码,偶然想起要回炉再来,就去HDU随便选了个最基础的题,也不记得曾经AC过:最后吃惊的发现,思路完全不一样了,代码风格啥的也有不小的变化.希望是成长了一点点吧.后面定期 ...

  4. 【ACM】魔方十一题

    0. 前言打了两年的百度之星,都没进决赛.我最大的感受就是还是太弱,总结起来就是:人弱就要多做题,人傻就要多做题.题目还是按照分类做可能效果比较好,因此,就有了做几个系列的计划.这是系列中的第一个,解 ...

  5. 【ACM】那些年,我们挖(WA)过的最短路

    不定时更新博客,该博客仅仅是一篇关于最短路的题集,题目顺序随机. 算法思想什么的,我就随便说(复)说(制)咯: Dijkstra算法:以起始点为中心向外层层扩展,直到扩展到终点为止.有贪心的意思. 大 ...

  6. 【Acm】算法之美—Crashing Balloon

    题目概述:Crashing Balloon On every  June 1st, the Children's Day, there will be a game named "crash ...

  7. 【ACM】不要62 (数位DP)

    题目:http://acm.acmcoder.com/showproblem.php?pid=2089 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新 ...

  8. 【Acm】八皇后问题

    八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题. 其解决办法和我以前发过的[算法之美—Fire Net:www.cnblogs.com/lcw/p/3159414.html]类似 题目:在8 ...

  9. 【ACM】hud1166 敌兵布阵(线段树)

    经验: cout 特别慢 如果要求速度 全部用 printf !!! 在学习线段树 内容来自:http://www.cnblogs.com/shuaiwhu/archive/2012/04/22/24 ...

随机推荐

  1. 使用poi读取word2007(.docx)中的复杂表格

    使用poi读取word2007(.docx)中的复杂表格 最近工作需要做一个读取word(.docx)中的表格,并以html形式输出.经过上网查询,使用了poi. 对于2007及之后的word文档,需 ...

  2. B1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 数学

    今天净做水题了,这个题还不到十五分钟就搞定了,思路特别简单,就是直接按照线性求因子个数的思路就行了. 题干: Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜 ...

  3. P2932 [USACO09JAN]地震造成的破坏Earthquake Damage 爆搜

    这题怎么这么水~~~本来以为挺难的一道题,结果随便一写就过了...本来还不知道损坏的牛棚算不算,结果不明不白就过了... 题干: 农夫John的农场遭受了一场地震.有一些牛棚遭到了损坏,但幸运地,所有 ...

  4. ecshop数据库说明

    数据库 ecshop 表的结构 ecs_account_log 字段 类型 空 默认 含义 log_id mediumint(8) 否 账户记录表 user_id mediumint(8) 否 用户编 ...

  5. Oracle创建用户教程

    计算机-->管理-->应用程序与服务-->(OracleOraDb11g_home1TNSListener 和 OracleServiceORCL 服务)->启动服务 打开Or ...

  6. IntelliJ IDEA/PyCharm/WebStorm 2019.1.2 注册码激活

    [IDEA2019.1.2最新版版本激活,直接查看底部] 网上IntelliJ IDEA激活方式大多均已失效,目前常用激活方式为License Server 激活: http://idea.imsxm ...

  7. 分别使用Hadoop和Spark实现TopN(1)——唯一键

    0.简介 TopN算法是一个经典的算法,由于每个map都只是实现了本地的TopN算法,而假设map有M个,在归约的阶段只有M x N个,这个结果是可以接受的并不会造成性能瓶颈. 这个TopN算法在ma ...

  8. weblogic详解

    一.简介 WebLogic是美国Oracle公司出品的一个application server,确切的说是一个基于JAVAEE架构的中间件,WebLogic是用于开发.集成.部署和管理大型分布式Web ...

  9. 【Python基础】while循环语句

    Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务.其基本形式为: while 判断条件: 执行语句…… 执行语句可以是单个语句或语句 ...

  10. Clocksource tsc unstable

    内核在启动过程中会根据既定的优先级选择时钟源.优先级的排序根据时钟的精度与访问速度. 其中CPU中的TSC寄存器是精度最高(与CPU最高主频等同),访问速度最快(只需一条指令,一个时钟周期)的时钟源, ...