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

    Let the Balloon Rise

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


    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
     
    Author
    WU, Jiazhi
     
    Source
     
    Recommend
    JGShining
     
     
     
     
    我当时做是这样的,把字符串按字典排序。然后用count[],str[].号码对应,分别是count[]某字符串的个数和str[]保存该字符串。然后找count[]中最大数对应的号码,再从号码中找那个串。
     
     
    后来看会自己的代码,发现还挺复杂的。人家的报告用STL才几行就OK了。
    所以一定要学习下STL
    我的代码:
    View Code
     1 #include<stdio.h>
    2 #include<string.h>
    3 #define maxn 1010
    4 char c[maxn][20];
    5 int main()
    6 {
    7 int n, i, j,k,max,count[maxn];
    8 char jh[20],str[maxn][20];
    9 while(scanf("%d",&n) == 1 && n)
    10 {
    11 getchar();
    12 for(i = 0;i < n;i++)
    13 {
    14 gets(c[i]);
    15 }
    16 for(i = 0;i < n - 1; i++)//排序,看!当时连快排都不会。
    17 {
    18 for(j = 0;j < n - 1 - i;j++)
    19 {
    20 if(strcmp(c[j],c[j + 1]) < 0)
    21 {
    22 strcpy(jh,c[j]);
    23 strcpy(c[j],c[j + 1]);
    24 strcpy(c[j + 1],jh);
    25 }
    26 }
    27 }
    28 memset(count,0,sizeof(count));
    29 count[0] = 1;
    30 strcpy(str[0],c[0]);
    31 k = 0;
    32 for(i = 1;i < n;i++)//计算每个串的个数。
    33 {
    34 if (strcmp(c[i],c[i - 1]) == 0)
    35 {
    36 count[k]++;
    37 }
    38 else
    39 {
    40 strcpy(str[++k],c[i]);
    41 count[k]++;
    42 }
    43 }
    44 max = 0;
    45 for(i = 1;i <= k;i++)//找个数最多的串对应的号码。
    46 {
    47 if(count[max] < count[i])
    48 {
    49 max = i;
    50 }
    51 }
    52 puts(str[max]);
    53 }
    54 return 0;
    55 }
    人家STL的代码:
    View Code
     1 #include<stdio.h>
    2 #include<string>
    3 #include<iostream>
    4 #include<map>
    5 using namespace std;
    6 const int MAXN=1000;
    7
    8 int main()
    9 {
    10 map<string,int>color;
    11 string cnt;
    12 string index;
    13 int i;
    14 int n;
    15 int max;
    16 while(scanf("%d",&n),n)
    17 {
    18 max=0;
    19 for(i=0;i<n;i++)
    20 {
    21 cin>>cnt;
    22 color[cnt]++;
    23 if(color[cnt]>max)
    24 {
    25 index=cnt;max=color[cnt];
    26 }
    27
    28 }
    29 cout<<index<<endl;
    30
    31
    32 }
    33 return 0;
    34 }
  • 相关阅读:
    491 · 回文数
    936 · 首字母大写
    1343 · 两字符串和
    1535 · 转换成小写字母
    13 · 字符串查找
    146 · 大小写转换 II
    241 · 转换字符串到整数(容易版)
    46 · 主元素
    kotlin协程——>通道
    kotlin协程——>异步流
  • 原文地址:https://www.cnblogs.com/qiufeihai/p/2323454.html
Copyright © 2011-2022 走看看