算法入门数学题hdoj 1004

1.数学题对于算法的要求不是很多,主要包括数学公式、数学定理,很适合做为研究算法和acm入门题型。hdoj 上面一些数学题分类:1004、1005、1008、1009 、1060

1012~1014、1019~1021 、1061、1049、1066 、1178、1108、1030 、1071、1597。


1004:是acm的一道比赛题目,可以使用STL,或者string类。stl代码比较简洁而且时间内存都较少具体如下:

TIME:0ms 

MEM: 364K 

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
    int n;
    while(cin>>n && n)
    {
        string temp;
        map<string,int> balloon;
        for(int i=0;i<n;i++)
        {
            cin>>temp;
            balloon[temp]++;
        }
        map<string,int>::iterator i=balloon.begin(),max=balloon.begin();

        for(;i!=balloon.end();i++)
        {
            if(i->second>max->second)
                max = i;
        }
        cout<<max->first<<endl;
    }

    return 0;
}

当然也可以使用结构体定义一个气球类:包括颜色和数量。

TIME:0ms 

MEM:372K

#include <iostream>
#include <string>


using namespace std;

struct balloon
{
    string color;
    int num;
}ballo[1000];

int main()
{
    int N;
    string temp;
    while(cin>>N &&N)
    {
        
        int len = 0;
        int max[2] = {0,0};//个数 数目
        bool flag = false;
        for(int i=0;i<N;i++)
        {
            cin>>temp;
            flag = false;
            for(int j=0;j<len;j++)
            {
                if(temp == ballo[j].color)
                {
                    ballo[j].num++;
                    flag = true;
                    if(ballo[j].num > max[0])
                    {
                        max[0] = ballo[j].num;
                        max[1] = j;
                    }
                }
                
            }
            if(flag == false)//没有
            {
                
                ballo[len].color=temp;
                ballo[len].num=1;
                len++;
            }
        }
        cout<<ballo[max[1]].color<<endl;
    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值