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

郁闷谁人知~

Posted by abilitytao at 2008-08-12 00:21:54 on Problem 2503
qsort and bsearch~但是我一直超时 调了两个小时 最后发现居然是一个小错误 无语~
不过我还有2个问题想请教各位达人
一是我用c++比G++时间少800多MS,why?(指的是下面的第一段代码之间比较(编译器不同))
二是我用另一段貌似差不多的代码,结果第二段代码(g++编译)和第一段代码用c++编译的时间差不多。吾甚不解,试请教之~
代码一
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char temp[1000]={0};
char eh[]={"eh"};
struct dic
{
	char enword[12];
	char foword[12];
};
dic a[100000];
int cmp(const void *a,const void *b)
{
	return strcmp(((dic*)a)->foword,((dic*)b)->foword);
}

/////////////////////////////////////////////////////////

int main ()
{
	int i,len=0;
	int front,rear,mid;
	for(i=0;;i++)
	{
	
		cin.getline(temp,1000);
		
		if(strlen(temp)==0)
			break;
		sscanf(temp,"%s%s",a[i].enword,a[i].foword);
		len++;
		
	}
	qsort(a,len,sizeof(a[0]),cmp);


	while(scanf("%s",temp)!=EOF)
	{
		front=0;
		rear=len-1;
	
		while(front<=rear)
		{
			mid=(front+rear)/2;
			if(strcmp(temp,a[mid].foword)==0)
			{
				printf("%s\n",a[mid].enword);
				break;
			}
			if(strcmp(temp,a[mid].foword)>0)
			{
				front=mid+1;
				continue;
			}
			else
			{
				rear=mid-1;
				continue;
			}
			
		}
		if(front>rear)
				printf("%s\n",eh);
		
	}
	return 0;
}
代码二
#include<iostream>
#include<cstring>
using namespace std;
char temp[25];

struct dic
{
	char enword[12];
	char foword[12];
};
dic a[100000];
int cmp(const void *a,const void *b)
{
	return strcmp(((dic*)a)->foword,((dic*)b)->foword);
}
int compare(const void *a,const void *b)
{
    return strcmp((char*)a,((dic*)b)->foword);
}


/////////////////////////////////////////////////////////

int main ()
{
	int i,len=0;
	
	for(i=0;;i++)
	{
	
		gets(temp);
		
		if(strlen(temp)==0)
			break;
		int j=0,k=0; 
	for(;temp[j]!=' ';j++)a[len].enword[j]=temp[j]; 
	a[len].enword[j]='\0'; 
	for(j++;temp[j]!='\0';j++,k++)a[len].foword[k]=temp[j]; 
	a[len].foword[k]='\0'; 
		

		len++;
		
	}
	qsort(a,len,sizeof(a[0]),cmp);


	while(scanf("%s",temp)!=EOF)
	{
		dic * pItem;
        pItem = (dic *)bsearch (temp, a, len, sizeof (dic), compare);
        if(pItem!=NULL)
            puts(pItem->enword);
        else printf("eh\n");
	}

		
	return 0;
}


				

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