hdu 1004 let the ballon rise

这一题相对前面两题要简单了许多,很容易想到用map,但是我还是不太适应oj思维,这是我一开始的代码,还没能做出来

#include<iostream>
#include<cstring>
#include<nap>

using namespace std;

int main()
{
int count = 0;
int i = 0;
string s;
cin >> count;
while(1)
{
if(count != 0)
{
map<int , string> m;
for(i = 0 ; i < count ; i++)
{
cin >> s;
int k = 10000;
for(auto a = m.begin() ; a != m.end() ; a++)
{
if(a -> second.compare(s) == 0)
{
k = a -> first;
break;
}
}
if(k != 10000)
{
m.insert(std::make_pair(1 , s);
}
else
{
m.insert(std::make_pair((a -> first)++ , s));
}
cout << m.end() -> second << endl;
}
else
return 0;
}
}
return 0;
}

问题在于,做oj题没必要存储数据,可以在读入数据的同时就进行计算过程了,还有就是对map的属性还不是很了解,所以做了很多无用功

这是之后的代码(ac)

#include<iostream>
#include<cstring>
#include<map>

using namespace std;

int main()
{
int t = 0;
while(cin >> t && t != 0)
{
map<string , int> m;
string s , s1;
int max = 0;
int i = 0;
for(i = 0 ; i < t ; i++)
{
cin >> s;
m[s]++;
if(m[s] > max)
{
max = m[s];
s1 = s;
}
}
cout << s1 << endl;
}
return 0;
}

猜你喜欢

转载自www.cnblogs.com/zzb-algorithm/p/11795956.html