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;
}