zoukankan      html  css  js  c++  java
  • HDU1004

    题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=1004

    Let the Balloon Rise

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

    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 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 using namespace std;
     5 
     6 char color[1111][20],ch[20];//color[i][]表示第i个气球的颜色是XX颜色
     7 int number[1111],n;//number[i]表示第i种颜色出现过几次
     8 
     9 int main ()
    10 {
    11     int i,j;
    12     int ok, max, t, k;
    13     while (scanf ("%d",&n)==1&&n)
    14     {
    15         memset(number, 0, sizeof(number));
    16         memset(color, '', sizeof(color));//初始化数组
    17         k = 0;
    18         for (i=0; i<n; i++)
    19         {
    20             scanf ("%s",ch);
    21             ok = 0;
    22             for (j=0; j<k; j++)
    23             {
    24                 if (strcmp(ch, color[j])==0)//如果改颜色之前出现过,+1
    25                 {
    26                     number[j]++;
    27                     ok = 1;
    28                 }
    29             }
    30             if (!ok)
    31             {
    32                 strcpy(color[k], ch);//如果改颜色没出现过,储存起来
    33                 number[k]++;
    34                 k++;
    35             }
    36         }
    37         max = 0;
    38         for (i=0; i<k; i++)//找到出现次数最多的颜色
    39         {
    40             if (number[i] > max)
    41             {
    42                 max = number[i];
    43                 t = i;//记录位置
    44             }
    45         }
    46         printf ("%s
    ",color[t]);
    47     }
    48     return 0;
    49 }
    View Code
  • 相关阅读:
    引用类型Array进行数值对比(应用jquery版)
    在网站前端中,你可能会用到的这些…
    javascript获得鼠标的坐标值
    增加PV方法
    wordpress后台修改“WordPress 地址(URL)”后无法打开的解决方法
    css给网页添加 黑白滤镜
    配置Android开发环境(fedora)
    成功的从fedora 7升级到了fedora 8
    听说这些是公司高管必读的笑话
    debian英文环境中中文输入
  • 原文地址:https://www.cnblogs.com/dxd-success/p/4235889.html
Copyright © 2011-2022 走看看