把字符串转化成数,存到int型数组中,然后排序。
注意:输入用gets,如果用cin会超时(一把辛酸泪)。需要补零。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int k=0;
const int maxn=1000005;
int la[maxn];
char a[256];
int main()
{
scanf("%d",&n);
for(int j=0;j<=n;j++)
{
gets(a);
long long num=0;
for(int i=0;i<=strlen(a);i++)
{
if(a[i]>='0'&&a[i]<='9')
num*=10,num+=a[i]-'0';
else if(a[i]>='A'&&a[i]<='C')
num*=10,num+=2;
else if(a[i]>='D'&&a[i]<='F')
num*=10,num+=3;
else if(a[i]>='G'&&a[i]<='I')
num*=10,num+=4;
else if(a[i]>='J'&&a[i]<='L')
num*=10,num+=5;
else if(a[i]>='M'&&a[i]<='O')
num*=10,num+=6;
else if(a[i]>='P'&&a[i]<='S')
num*=10,num+=7;
else if(a[i]>='T'&&a[i]<='V')
num*=10,num+=8;
else if(a[i]>='W'&&a[i]<='Y')
num*=10,num+=9;
}la[++k]=num;
}
sort(la+1,la+k+1);
int ha=0;
bool flag=0;
for(int i=2;i<=k+1;i++)
{
if(la[i]!=la[i-1])
{
if(ha>=2)
{
flag=1;
printf("%03d",la[i-1]/10000);printf("-");
printf("%04d",la[i-1]%10000);
cout<<' '<<ha<<endl;
}ha=1;
}
else ha++;
}
if(flag==0) cout<<"No duplicates."<<endl;
return 0;
}