| ||||||||||
| 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 | |||||||||
诸位,帮我看看我1002题,为何是wrong answer?#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 100
const int chars[26]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9,0};
char * standardPhone(char * phone){
int len=strlen(phone);
//printf("phone=%s,len=%d\n",phone,len);
int i;
char c;
char * stPhone=(char *)malloc(sizeof(char)*9);
int pos=0;
for(i=0;i<len;i++){
c=phone[i];
//printf("c=%c,pos=%d\n",c,pos);
if(c>='0'&&c<='9'){
stPhone[pos++]=c;
}else if( c>='A'&&c<='Z'){
stPhone[pos++]=chars[c-'A']+'0';
}
if(pos==3)
stPhone[pos++]='-';
}
stPhone[8]=0;
//printf("stPhone=%s\n",stPhone);
return stPhone;
}
void printPhoneSets(char **phoneSets,int len){
int i;
for(i=0;i<len;i++){
printf("%s\n",phoneSets[i]);
}
}
//for quick sort,using c function qsort
int icmp(const void *p1,const void *p2){
return strcmp(*(char * const *)p1,*(char * const *)p2);
}
//end of for quick sort,using c function qsort
void output(char **phoneSets,int len){
char * curr=phoneSets[0];
int i,dup=1;
for(i=1;i<len;i++){
if(!strcmp(curr,phoneSets[i]))
dup++;
else{
if(dup>1){//output
printf("%s %d\n",curr,dup);
}
dup=1;
curr=phoneSets[i];
}
}
}
int main(){
int phoneNums;
char phone[LEN];
char **phoneSets;
int i=0;
scanf("%d",&phoneNums);
if(phoneNums==0||phoneNums==1){
printf("No duplicates.\n");
}else{
phoneSets=(char **)malloc(sizeof(char *)*phoneNums);
for(i=0;i<phoneNums;i++){
scanf("%s",phone);
*(phoneSets+i)=standardPhone(phone);
}
//printPhoneSets(phoneSets,phoneNums);
qsort(phoneSets,phoneNums,sizeof(phoneSets[0]),icmp);
//printf("sorted:\n");
//printPhoneSets(phoneSets,phoneNums);
output(phoneSets,phoneNums);
free(phoneSets);
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator