Let the Balloon Rise

http://acm.hdu.edu.cn/showproblem.php?pid=1004

View Code
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std ;
struct colorball
{
    int num ;
    char colors[16] ;
} ;
struct colorball a[1000] = {0, ""} ;
int cmp(const void *a, const void *b)
{
    return (*(int*)b - *(int*)a) ;
}
int main()
{
    int n, i, j ;
    char s[16] ;
    bool flag ;
    while(cin>>n, n!=0)
    {
        int count = 0 ;
        for(i=0; i<n; i++)
        {
            cin>>s ;
            flag = false ;
            for(j=0; j<count; j++)
            {
               if(strcmp(s, a[j].colors)==0)
               {
                   flag = true ;
                   a[j].num++ ;
               }
            }
            if(!flag)
            {
                strcpy(a[count].colors, s) ;
                a[count].num = 0 ;
                count++ ;
            }
        }
        qsort(a, count, sizeof(colorball), cmp) ;
        cout<<a[0].colors<<endl ;
    }
    return 0 ;
}

 一般的做法:

View Code
#include<iostream>
#include<cstring>
using namespace std ;
int main()
{
    int n, num[1000], i, j ;
    char s[16], colors[1000][16] ;//注意用二维数组
    while(cin>>n, n!=0)
    {
        int k = 0;
        memset(num, 0, sizeof(num)) ;
        for(i=0; i<n; i++)
        {
            cin>>s ;
            for( j=0; j<k; j++)
            {
                if(strcmp(s, colors[j])==0)
                {
                    num[j]++ ;
                    break ;
                }
            }
            if( j==k)
            {
                num[k] = 1 ;
                strcpy(colors[k], s ) ;
                k++ ;
            }
        }
        int max = 0 ;
        int t = 0 ;
        for(int j=0; j<k; j++)
        {
            if(num[j]>max)
            {
                max = num[j] ;
                t = j ;
            }
        }
        cout<<colors[t]<<endl ;
    }
    return 0 ;
}
原文地址:https://www.cnblogs.com/yelan/p/3019069.html