1004-Let the Balloon Rise

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

#include <iostream>
#include<cstring> 
#include<algorithm>
using namespace std;  

//颜色结构,num用来计算每种颜色出现的次数   
struct Color{  
    int num;  
    char color[16];   
};  

Color input[1000] = {0,""};  

//按降序排序   
int int_cmp(const void *a, const void *b)  
{  
    return *(int *)b - *(int *)a;  
}  

int main()  
{  
    //freopen("E:\\in.txt","r",stdin);  
    int n,i,j;  
    char temp[16];  
    while(cin>>n,n!=0)  
    {  
        int count=0;  
        bool exist;  
        for(i=0; i<n; i++){  
//每读入一个颜色,就判断是否之前出现过,如果出现过,就把exist标记为true,并把计数+1,然后立即break出循环   
            cin >> temp;  
            exist = false;  
            for(j=0; j<count; j++){  
                if(strcmp(temp,input[j].color) == 0){//在所有读入不同的颜色中查找  
                    exist = true;  
                    input[j].num++;  
                    break;  
                }  
            }  
            //如果不存在,就把这种颜色加到数组中去,并使计数初始为0   
            if(!exist)  {  
                //input[count].color = temp;  
                strcpy(input[count].color,temp);  
                input[count].num = 1;  
                count++; //表示读入不同颜色的个数 
            }  
        }  
        //对数组进行快速排序,排在第一个的就是数量最多的颜色,最后打印   
        qsort(input,count,sizeof(Color),int_cmp);  //颜色个数从大到小排序 
        cout << input[0].color << endl;     
    }  
    return 0;  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值