zoukankan      html  css  js  c++  java
  • HDU 1004 Let the Balloon Rise

    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
     
    题意:先输入一个整数(1--1000),表示输入的表示颜色的字符串的个数,输出重复次数最多的颜色。
    分析:问题的核心是输入字符串,并将其输入到二维字符数组中,然后用二从循环计算颜色重复的次数m,与max(初始值为1)比较,保持max始终最大,同时m与max交换树值的时候,将表示该颜色的字符串的首地址赋予js,最后输出js中的字符串即可。
    AC源代码(C语言):
     1 #include<stdio.h>
     2 #include<string.h>
     3 int main()
     4 {
     5   int i,j,m,n,s,max,js;      //i和j是循环控制符,m用于表示每种颜色出现的次数,n代表输入颜色的次数,s存放n的值,max表示出现颜色最多的次数,js存放出现次数最多的颜色的数组下标 
     6   char a[1000][15],b[15];
     7   while(scanf("%d",&n)&&n)  //因为后面要输入字符串,要防止数组接受\n
     8     { 
     9       max=1;                //注意初始化max的位置和初始化m的位置 
    10       s=n;      
    11       for(i=0;i<s;i++)     //输入字符串,并将其存储在二维数组中
    12        {
    13          scanf("%s",&b);
    14          strcpy(a[i],b);               
    15        }
    16       for(i=0;i<s;i++)    //计算字符串出现的次数 
    17        { 
    18          m=1;             //注意语句位置 
    19          for(j=i+1;j<s;j++)
    20           {
    21             if(a[i][0]=='\0') continue;   //与下面”格式化“ 成的语句保持一致 
    22             if(strcmp(a[i],a[j])==0)
    23               {
    24                 m++;                      
    25                 a[j][0]=='\0';     //重复的颜色要“格式化”,比如赋值1,2,3......,这里赋值‘\0’            
    26               }               
    27             if(m>=max)           //将颜色出现最多的数组下标赋予js。要注意这个语句在for语句里面! 
    28               {
    29                 max=m;        
    30                 js=i;      
    31               }          
    32           }
    33       }  
    34      puts(a[js]);   
    35     } 
    36   return 0;       
    37 }

    2013-03-13

  • 相关阅读:
    大端序与小端序
    中断分类
    PHP开发框架[国内框架]
    PHP开发框架[流行度排名]
    ecshop 后台分页功能
    Windows下phpStudy中的Apache无法启动的排查方法
    Windows里配置Apache2.2+PHP5.3+mod_fcgid运行高效的FastCGI模式
    Apache多虚拟主机多版本PHP(5.2+5.3+5.4)共存运行配置全过程
    让 Node.js 支持 ES6 的语法
    微信小程序请求wx.request数据,渲染到页面
  • 原文地址:https://www.cnblogs.com/fjutacm/p/2956529.html
Copyright © 2011-2022 走看看