通过这道题发现了一些好玩的东东,顺便复习了很多东西!
先贴下代码!这个代码几乎来自网络!还有一些人用排序二叉树,字典树,快排做!真是牛叉!
#include <iostream>
#include <queue>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;
priority_queue<int, vector<int>, greater<int> >q;
int t[10000000];
int main()
{
int i,j,N,len,k;
int al[26]= {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
char s1[100],s2[100];
while(scanf("%d",&N)!=EOF)
{
memset(t,0,sizeof(t));
while(N --)
{
scanf("%s",s1);
len = strlen(s1);
k = 0;
for(i = 0;i < len;i ++)
{
if(s1[i] != '-')
{
if(s1[i]>='A'&&s1[i]<'Z')
s2[k++] = char(al[s1[i] - 'A']+'0');
else
s2[k++] = s1[i];
}
}
bool flag = 0;
j =atoi(s2);
t[j] ++;
if(t[j] == 2) q.push(j);
}
if(!q.empty()){
while(!q.empty())
{
int s = q.top();
char str[10];
sprintf(str,"%07d",s);
for(i = 0;i < 7;i ++)
{
if(i == 2) printf("%c-",str[i]);
else printf("%c",str[i]);
}
printf(" %d\n",t[s]);
q.pop();
}
}
else printf("No duplicates.\n");
}
return 0;
}
知识点:
1、int 转成string:
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
int test = 10;
string line = "";
ostringstream ost(line);
ost << test;
cout << ost.str() << endl;
return 0;
}
2、string转成 int
#include <string>
string str="12345";
int b=atoi(str.c_str());
3、sprintf() 的用法,将char[] 转成 int;
4、优先队列对整数的上升排序,可以转成对负数的下降排序。
5、优先队列对string可排序!按的应该是字典顺序!(需确认)