[ACM]pku1002

本文介绍了一个C语言程序,用于处理电话号码数据,通过将字母转换为对应的数字,并统计每个电话号码出现的次数。该程序首先定义了电话号码的结构体,并实现了一套转换规则将包含字母的电话号码标准化为纯数字格式,最后通过排序和比较来统计重复号码的数量。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 100000 //电话号码最多个数
typedef struct {char elem[8];int time;} item;//电话号码结构体,包括号码字符串、同样串出现次数
item ph[MAX];//记录实际输入的号码数
/*


A, B, and C map to 2
D, E, and F map to 3
G, H, and I map to 4
J, K, and L map to 5
M, N, and O map to 6
P, R, and S map to 7
T, U, and V map to 8
W, X, and Y map to 9
*/转换规则
char hash(char ch)//把字符转成数字的ASCII码eg:7+‘0’表示7的ASCII码
{
 if(ch<'P') return (char)((ch-'A')/3+2+'0');
 else if(ch>'S'&&ch<'Z') return (char)((ch-'T')/3+8+'0');
 else return (char)(7+'0');  
}

void trans(char * str,int ii)//把原始输入串转成一般规范数字形式存入ph数组中
{
 int i = 0,j = 0;
 while(str[i]!='/0')
 {
  if(str[i]=='-') {i++; continue;}
  else if(str[i]>='A'&&str[i<'Z']) ph[ii].elem[j++]=hash(str[i++]);
  else ph[ii].elem[j++]=str[i++];
 }
 ph[ii].elem[j]='/0';
 ph[ii].time = 1;
}
int cmp(const void * a,const void * b)
{
 return strcmp(((item *)a)->elem,((item *)b)->elem);
}
void main()
{
 int i=0,j=0,n;
 int fg=1;
 char s[100];
 scanf("%d",&n);
 getchar();
 while(i<n)
 {
  scanf("%s",s);
  trans(s,i);
  i++;
 }
 qsort(ph,n,sizeof(item),cmp);
 //ph中的各个串已排好序
 i=0,j=0;
 while(i++<n)
 {
  if(strcmp(ph[i].elem,ph[j].elem)!=0)
  {
   if(ph[j].time >1)
   {
    printf("%c%c%c-%c%c%c%c %d/n",ph[j].elem[0],ph[j].elem[1],ph[j].elem[2],
     ph[j].elem[3],ph[j].elem[4],ph[j].elem[5],ph[j].elem[6],ph[j].time);
    fg=0;
   }
   j=i;
  }
  else ph[j].time+=ph[i].time;
 }

//j指向前面的,i指向后面的,在已经排序的数组中,i、j所指串相同则把j的time加上i的,然后j再从i的位置继续往后走 
 if(fg) printf("No duplicates./n");
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值