题意:统计电话号码个数,并按字典序输出,map 完美的解决
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
const int MAXN = 201;
int nCases;
char num[] = "22233344455566677778889999";
map<string,int >tel;
int main()
{
int i,j;
cin>>nCases;
while(nCases--)
{
tel.clear();
int nums;
cin>>nums;
for ( i = 0 ; i < nums ; i++)
{
string temp,trans;
cin >> temp ;
trans = "" ;
int k = 0 ;
for ( j = 0 ; j < temp.size() ; j++)
{
if ( k == 3 )
{
trans += "-";
k++;
j--;
continue;
}
if (isupper(temp[j]))
{
char ch = num[temp[j]-'A'];
trans += ch ;
k++;
continue;
}
if (temp[j] == '-')
continue;
trans += temp[j];
k++;
}
tel[trans]++; //字符串对应的数值+1
}
bool flag=true;
map<string,int>::iterator it = tel.begin();
while (it != tel.end())
{
if (it->second >= 2)
{
cout<<it->first<<" "<<it->second<<endl;
flag = false ;
}
it++;
}
if (flag)
cout << "No duplicates." << endl;
if (nCases != 0)
cout<<endl;
}
return 0;
}