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 |
自己用TC都调试通过了,可为什么老是WA呢?实在搞不明白,高手帮帮测试一下吧。不慎感激~~~#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct { char teleNum[8]; }TeleNum; typedef struct { char teleNum[8]; /*相同类电话号码的一个代表*/ int total; /*相同的电话号码数*/ }Result; void quicksort(Result rt[],int low,int high) { int i,j; char str[8]; if(low<high) { strcpy(str,rt[low].teleNum); i=low; j=high; while(i<j) { while(i<j&&strcmp(rt[j].teleNum,str)>0) j--; if(i<j) strcpy(rt[i++].teleNum,rt[j].teleNum); while(i<j&&strcmp(rt[i].teleNum,str)<=0) i++; if(i<j) strcpy(rt[j--].teleNum,rt[i].teleNum); } strcpy(rt[i].teleNum,str); quicksort(rt,low,i-1); quicksort(rt,i+1,high); } } int main() { unsigned long teleSum; /*实际的电话号码数*/ unsigned int i,j,k,m=0; /*辅助变量*/ /* //TeleNum numbers[1]; //电话号码串数组 //Result result[1]; //标示每一组电话号码串 //Result temp[1]; //辅助变量*/ char c; /*//辅助变量*/ TeleNum* numbers; /*numbers:电话号码串数组,是一个TeleNum类型的指针,也可以理解为指向TeleNum数组的首地址*/ Result* result; /*result:标示每一组电话号码串,是一个Result类型的指针,也可以理解为指向Result数组的首地址*/ Result* temp; /*temp:辅助变量,是一个Result类型的指针,也可以理解为指向Result数组的首地址*/ scanf("%uld",&teleSum); c=getchar(); numbers=(TeleNum *)malloc(sizeof(TeleNum)*teleSum); result=(Result *)malloc(sizeof(Result)*teleSum); temp=(Result *)malloc(sizeof(Result)*teleSum); if(numbers==NULL||result==NULL||temp==NULL) { printf("Memery allocation failed!\n"); exit(1); } /** 完成teleSum行电话号码的输入 */ for(i=0;i<teleSum;i++) { for(j=0;j<7;j++) { c=getchar(); while(c=='-'||c=='\n') c=getchar(); numbers[i].teleNum[j]=c; } numbers[i].teleNum[j]='\0'; } /*完成电话号码中字母到数字的转换 */ for(i=0;i<teleSum;i++) { for(j=0;j<8&&numbers[i].teleNum[j]!='\0';j++) { switch(numbers[i].teleNum[j]) { case 'A': case 'B': case 'C':c='2';break; case 'D': case 'E': case 'F':c='3';break; case 'G': case 'H': case 'I':c='4';break; case 'J': case 'K': case 'L':c='5';break; case 'M': case 'N': case 'O':c='6';break; case 'P': case 'R': case 'S':c='7';break; case 'T': case 'U': case 'V':c='8';break; case 'W': case 'X': case 'Y':c='9';break; default:c=numbers[i].teleNum[j];break; } numbers[i].teleNum[j]=c; } } /*比较电话号码串*/ for(k=0,i=0;i<teleSum;i++) { if(strcmp(numbers[i].teleNum,"null")!=0) { result[k].total=1; for(j=i+1;j<teleSum;j++) { if(strcmp(numbers[j].teleNum,"null")!=0) if(strcmp(numbers[i].teleNum,numbers[j].teleNum)==0) { strcpy(numbers[j].teleNum,"null"); result[k].total++; } } strcpy(result[k].teleNum,numbers[i].teleNum); k++; strcpy(numbers[i].teleNum,"null"); } } /*找出总数大于1的电话号码记录,并赋值给临时变量temp*/ for(i=0;i<k;i++) { if(result[i].total>1) { strcpy(temp[m].teleNum,result[i].teleNum); temp[m].total=result[i].total; m++; } } if(m==0) { printf("\nNo duplicates."); } else { for(i=0;i<m;i++) { strcpy(result[i].teleNum,temp[i].teleNum); result[i].total=temp[i].total; } /*对结果记录按照电话号码从低到高进行排序*/ quicksort(result,0,m-1); printf("\n"); for(i=0;i<m;i++) { for(j=0;j<3;j++) putchar(result[i].teleNum[j]); putchar('-'); for(j=3;j<7;j++) putchar(result[i].teleNum[j]); putchar(' '); printf("%d\n",result[i].total); } } free(numbers); free(result); free(temp); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator