hdoj 1004

#include<iostream>
#include<string>
using namespace std;
struct bal_col
{
    string color;
    int amount;
};
int main()
{
    void reset(bal_col a[]);   //重置存储气球颜色的数组
    int    pop_bal_index(bal_col a[], int);//求含有最受欢迎的气球颜色的数组元素的索引
    bal_col a[1000];//定义一个数组来存储输入的气球颜色
    reset(a);
    int total, i;
    string temp;
    while(cin>>total && total != 0)
    {
        int _total = total;
        int kinds = 0;//气球颜色种类
        while(_total --)
        {
            cin>>temp;
            if(kinds == 0)
            {
                a[0].color = temp;
                a[0].amount = 1;
                kinds++;
            }
            else
            {
                bool dif_from_all = true;//先假设输入的气球颜色temp与已有的所有气球的颜色都不同
                for(i = 0; i < kinds; i++)
                {
                    if(a[i].color == temp)
                    {
                        dif_from_all = false;
                        a[i].amount++;
                        continue;
                    }
                }
                if(dif_from_all) //若不同,则用数组中的下一元素来存储这个颜色
                {
                    a[kinds].color = temp;
                    a[kinds].amount++;
                    kinds++;
                }
            }
        }
        int POP_INDEX = pop_bal_index(a, kinds);
        cout<<a[POP_INDEX].color<<endl;
        reset(a);
    }
    return 0;
}
void reset(bal_col a[])
{
    int i;
    for(i = 0; i < 1000; i++)
    {
        a[i].color = " ";
        a[i].amount = 0;
    }
}
int    pop_bal_index(bal_col a[], int n)
{
    int j;
    int max_amount = 0;
    int pop_index = 0;
    for(j = 0; j < n; j++)
    {
        if(a[j].amount > max_amount)
        {
            max_amount = a[j].amount;
            pop_index = j;
        }
    }
    return pop_index;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值