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 |
为什么老超时啊,哪位大牛帮小菜看看,急人啊#include<stdio.h> #include<string.h> #include<stdlib.h> struct no { int data; char a[50]; }; typedef struct no NODE; void change(char c[]); void show(NODE*,int); int compare(const void *p1,const void *p2) { return strcmp((*(NODE *)p1).a,(*(NODE *)p2).a); } void main() { int n; int i,k; int tag=0; int temp=0; char c[20]; NODE *p; scanf("%d",&n); getchar(); p=(NODE*)malloc(n*sizeof(NODE)); p[temp].data=0; strcpy(p[temp].a,"\0"); for(i=0;i<n;i++) { change(c); for(k=0;k<temp;k++) if(strcmp(p[k].a,c)==0) { p[k].data++; tag=1;break; } if(!tag) { strcpy(p[temp].a,c); p[temp].data++; temp++; p[temp].data=0; } tag=0; } temp--; qsort(p,temp,sizeof(p[0]),compare); show(p,temp); } void show(NODE*p,int temp) { int i=0,j,tag=0; while(i<=temp) { if(p[i].data>1) { tag=1; for(j=0;j<(int)strlen(p[i].a);j++) { printf("%c",p[i].a[j]); if(j==2) printf("-"); } printf(" %d\n",p[i].data); } i++; } if(!tag) printf("No duplicates.\n"); } void change(char c[]) { unsigned int i; char ch; for(i=0;i<20;i++) { ch=getchar(); if(ch=='\n') break; if(ch=='-') { i--; continue; } else if(ch>='A'&&ch<='P') c[i]=(ch-'A')/3+'2'; else if(ch>='R'&&ch<='Y') c[i]=(ch-'B')/3+'2'; else if(ch>='0'&&ch<='9') c[i]=ch; } c[i]='\0'; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator