算法入門數學題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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章