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 |
堆排序 320MS 二叉排序 540MS 字符数组必须开大 我开到260 了代码部分。 注释的是堆排序 没注释的二叉树 #include<stdio.h> #include<stdlib.h> typedef struct node{ int mid; int number; struct node *left; struct node *right; }BSTree; int flag; int Tidy(char phone[]); BSTree *BinFind(BSTree *btree,int key); int CaretTree(BSTree *btree,int mid); int BinSort(BSTree *btree); int main(){ BSTree *btree,*addres; char phone[250]; int n,value,i; scanf("%d",&n); scanf("%s",phone); value = Tidy(phone); btree =( BSTree *)malloc(sizeof(BSTree)); btree ->left=btree ->right=NULL; btree ->mid = value; btree ->number=1; flag = 1; for( i = 1 ; i < n;i++){ scanf("%s",phone); value =Tidy(phone); addres = BinFind(btree,value); if ( addres ){ addres ->number ++; }else{ CaretTree(btree,value); } } BinSort(btree); if( flag ) printf("No duplicates.\n"); return 0; } int Tidy(char phone[]){ char ch; int j,i,k; k=i=0; while(phone[i]){ ch=phone[i]; if(ch>='0'&&ch<='9'){ k=k*10+(ch-'0'); }else{ if(ch>='A'&&ch<='P'){ j=(ch-'A')/3+2; k=k*10+j; }else{ if(ch>='R'&&ch<='Y'){ j=(ch-'A'-1)/3+2; k=j+k*10; } } } i++; } return k; } BSTree *BinFind(BSTree *btree,int key){ if( ! btree || btree ->mid == key){ return btree; }else{ if( btree ->mid > key) return (BinFind(btree ->left,key)); else return (BinFind(btree ->right,key)); } } int CaretTree(BSTree *btree,int mid){ BSTree *head,*parent,*node; node = (BSTree *)malloc(sizeof(BSTree)); node ->left = node ->right = NULL; node ->mid = mid; node ->number = 1; head = btree; while( head ){ parent =head; if( head ->mid > mid ) head= head ->left; else head = head ->right; } if( parent ->mid > mid) parent ->left =node; else parent ->right =node; return 0; } int BinSort(BSTree *btree){ if( btree){ BinSort(btree->left); if( btree ->number > 1){ printf("%03d-%04d %d\n",btree->mid/10000,btree->mid%10000,btree->number); flag = 0 ; } BinSort(btree ->right); } return 0; } /* #include<stdlib.h> #include<stdio.h> int tidy_phonenumber(char telphone[],int *number); ///*字符串处理部分 int HeapAdjust(int mid[],int start,int n); ///*构造堆排序的函数 int HeapSort(int mid[],int n); ///*堆排序函数 int main(){ int n,i,j,k,flag=1,*mid; char phone[250]; scanf("%d",&n); mid=(int *)malloc(n*sizeof(int)); ///*动态申请空间避免空间浪费 for(i=0;i<n;i++){ scanf("%s",phone); tidy_phonenumber(phone,&mid[i]); } HeapSort(mid,n); // /*排序部分 // /*下面的对已经排序的好的数组进行扫描输出我们需要的 // /*扫描部分自我感觉还可以优化 for(i=0;i<n;){ if(mid[i]==mid[i+1]){ for(j=i,k=1;mid[j]==mid[j+1];){ j++; k++; } printf("%03d-%04d %d\n",mid[i]/10000,mid[i]%10000,k); i+=k; flag=0; }else{ i++; } } if(flag) //如果没有相同的情况下输出。我想也不可能没有相同 printf("No duplicates.\n"); free(mid);///释放空间 return 0; } int tidy_phonenumber(char telphone[],int *number){ char ch; int j,i,k; k=i=0; while(telphone[i]){ ch=telphone[i]; if(ch>='0'&&ch<='9'){ k=k*10+(ch-'0'); }else{ if(ch>='A'&&ch<='P'){ j=(ch-'A')/3+2; k=k*10+j; }else{ if(ch>='R'&&ch<='Y'){ j=(ch-'A'-1)/3+2; k=j+k*10; } } } i++; } *number=k; return 0; } int HeapSort(int mid[],int n){ int i,j,value; for(i=n/2-1;i>=0;i--){ HeapAdjust(mid,i,n); } for(i=n-1;i>0;i--){ value=mid[0]; mid[0]=mid[i]; mid[i]=value; HeapAdjust(mid,0,i); } return 0; } int HeapAdjust(int mid[],int start,int n){ int i,j; while(2*start+1<n){ j=2*start+1; if((j+1)<n){ if(mid[j]<mid[j+1]) j++; } if(mid[start]<mid[j]){ i=mid[start]; mid[start]=mid[j]; mid[j]=i; start=j; }else{ break; } } return 0; } */ Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator