| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
求助 answer wrong~~~~~~~~我的源代码,初学者,C语言
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 20
void del(char*);
void change(char*);
void add(char*);
char *book(int);
int *Count(int);
void up(char*,int);
void RepeatTimes(char*,int*,int);
void main()
{
int n,i,*count;
char *p;
scanf("%d",&n);
getchar();
p=book(n);
count=Count(n);
for(i=0;i<n;i++)count[i]=0; /*令计数器数组初始化为0*/
if(p==NULL)
{
printf("内存不足!");
exit(0);
}
else
{
for(i=0;i<n;i++)
{
gets(&p[i*LEN]);
del(&p[i*LEN]);
change(&p[i*LEN]);
add(&p[i*LEN]);
}
up(p,n);
RepeatTimes(p,count,n);
}
for(i=0;i<n;i++)
{
if(count[i]>1)
{
printf("%s ",&p[i*LEN]);
printf("%d\n",count[i]);
}
}
}
/*函数功能:删除连接符"-"*/
void del(char *a)
{
int i=0,j=0;
for (;a[i]!=0;i++)
{
if(a[i]!='-')
{
a[j]=a[i];
j++;
}
}
a[j]=0;
}
/*函数功能:申请一个长度n行LEN宽的动态字符二维数组。
返回数组首(类型为char*)地址。用于存放电话号码*/
char *book(int n)
{
char *p=NULL;
if(n<=0||n>=100000)
{
printf("输入错误!");
exit(0);
}
return (char*)calloc(n*LEN,sizeof(char));
}
/*函数功能:申请一个长度为n的动态一维数组作为计数器。
返回数组首地址(类型int*).*/
int *Count(int n)
{
return (int*)calloc(n,sizeof(int));
}
/*函数功能:将输入的电话号码中的英文字母转换成对应
的数字(字符型)。*/
void change(char *b)
{
int i;
for(i=0;b[i];i++)
{
if(b[i]>=65&&b[i]<=67)b[i]='2';
if(b[i]>=68&&b[i]<=70)b[i]='3';
if(b[i]>=71&&b[i]<=73)b[i]='4';
if(b[i]>=74&&b[i]<=76)b[i]='5';
if(b[i]>=77&&b[i]<=79)b[i]='6';
if(b[i]=='P'||b[i]=='S'||b[i]=='R')b[i]='7';
if(b[i]>=84&&b[i]<=86)b[i]='8';
if(b[i]>=87&&b[i]<=89)b[i]='9';
}
}
/*函数功能:在指定的第4位添加连接符"-"*/
void add(char *c)
{
int i;
for(i=7;i>=3;i--)
c[i+1]=c[i];
c[3]='-';
}
/*函数功能:对存放电话号码的数组执行升序排列操作*/
void up(char *d,int n)
{
int i,j;
char temp[LEN];
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(strcmp(&d[i*LEN],&d[(j+1)*LEN])>0)
{
strcpy(temp,&d[i*LEN]);
strcpy(&d[i*LEN],&d[(j+1)*LEN]);
strcpy(&d[(j+1)*LEN],temp);
}
}
}
}
/*函数功能:计算的出电话号码的重复次数,存放于对应的计数器数组元素中*/
void RepeatTimes(char *book,int count[],int n)
{
int i,j,flag;
for(i=0;i<n;i++)
{
flag=1;/*设置标志位,记1为无重复*/
for(j=0;j<i;j++)
{
if(!strcmp(&book[j*LEN],&book[i*LEN]))
{
count[j]++;
flag=0;
break;
}
}
if(flag)count[i]++;
}
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator