Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:qsort的cmp函数应该怎么改?

Posted by 8085 at 2006-03-12 12:16:30 on Problem 1002
In Reply To:Re:qsort的cmp函数应该怎么改? Posted by:8085 at 2006-03-12 12:13:37
> 谢拉~~~~~~~~~~~~~,用了qsort可惜还是超时唉
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
struct tel
{
		int cishu;
		char telnum[16];
}phone[100001];
int cmp(const void * a,const void * b)
{
		return strcmp( (*(tel*)a).telnum,(*(tel*)b).telnum);

}
void main()
{
	int num,i=0,len,j,k=0;
	char con[]="22233344455566677778889999";
	cin >>num;
	for(;i<num;i++)
	{
		cin >>phone[i].telnum;
		phone[i].cishu=1;;
		len=strlen(phone[i].telnum);
		for(j=0;j<len;j++)
		{
			if(phone[i].telnum[j]=='-')
			{
				for(k=j;k<len;k++)
					phone[i].telnum[k]=phone[i].telnum[k+1];
				len--;     
			}
			if(phone[i].telnum[j]>='A'&&phone[i].telnum[j]<='Y')
				phone[i].telnum[j]=con[phone[i].telnum[j]-'A'];
		}
		for(j=0;j<i;j++)
			if(strcmp(phone[j].telnum,phone[i].telnum)==0)
			{
				phone[j].cishu++;
				phone[i].cishu=-100000;
			}
	}
	qsort(phone,num,sizeof(phone[0]),cmp);
	for(j=0;j<num;j++)
		{
			if(phone[j].cishu>1)
			{
				k++;
				for(int x=0;x<7;x++)
				{
					if(x==3)
						cout <<'-';
					cout <<phone[j].telnum[x];
				}
				cout <<' '<<phone[j].cishu<<endl;
			
			}
		}
	if(k==0) cout <<"No duplicates."<<endl;
	
}



Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator