POJ题目487-3279
关键点:1 将输入号码转换成标准号码过程中,字母对应可事先设置一个map数组,提前设好对应策略
2 学会使用qsort()函数
代码(参考了标答):
// acm.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "math.h"
//#include <string>
#include <algorithm>//n*log2(n)
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int compare(const void *elem1,const void *elem2)
{
//为函数模板 sort 定义数组元素的比较函数
return (strcmp((char*)elem1, (char*)elem2));
};
//int compElem(char elem1,char elem2)
char map[]="22233344455566677778889999";
void main()
{
int n,i,j,k;
char telNums[100000][9];/存标准号码
char temp[20];存当前输入号码
scanf("%d",&n);///样例个数
for(i=0;i<n;i++)
{
scanf("%s",temp);
j=0;
k=0;
while(temp[k]!='\0'&&j<9)//从当前输入整理出号码的标准形式,
//并存在telNums数组的一横行里
{
if(j==3)
{
telNums[i][j]='-';
j++;
}
if(temp[k]>='0'&&temp[k]<='9')
{
telNums[i][j]=temp[k];
j++;
//k++;
}
else if(temp[k]>='A'&&temp[k]<='Z')
{
telNums[i][j]=map[temp[k]-'A'];
j++;
}
k++;
}
telNums[i];
telNums[i][j]='\0';
}
qsort(telNums,n,9,compare); //对所有输入的数组进行排序,以数组的每一行为单位
telNums;
for(i=0;i<n;)/利用循环,找出相邻每行的重复次数即该号码
//的重复次数
{
k=1;
for(j=i+1;j<n;j++)
{
if(strcmp(telNums[i],telNums[j])==0)//这里[i]后面不用跟[]了
k++;
else
break;
}
if(k>1)
printf("%s%c%d\n",telNums[i],' ',k);
i+=k;
}
}