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

各位兄台,TLE的code,看看那能不能优化。

Posted by RUNSLOWLY at 2008-09-07 14:51:30 on Problem 1142
#include<stdio.h>
#include<math.h>
int f(__int64 n)
{
	__int64 i;
	i=0;
	while(n/10!=0)
	{
		i+=n%10;
		n=n/10;
	}
	i+=n%10;
//	printf("%I64d\n",i);
	return (int)i;
}

int prime(__int64 n)
{
	__int64 i,max;
	int  sum,mid;
	sum=0;
	mid=-1;
	if(n<=1)
	{
		printf("input erro\n");
		return -1;
	}
	if(n==2||n==3)
	{
		return -1;
	}
	else
	{
		max=(int)sqrt((double)n);//等一下看看能不能优化。
		for(i=2;i<=max;i++)
		{
			if(n%i==0)
			{
				sum+=f(i);
				n=n/i;
				mid=0;
				max=n;
				i--;
			}
			else
				if(mid!=0)
			    	mid=-1;
		}
	}
	if(mid==-1)
		return mid;
	else
		return sum;
}
int main()
{

	__int64 n;
	__int64 i;
	while(1)
	{
		scanf("%I64d",&n);
		if(!n) break;
		i=1;
		while(1)
		{
			if(prime(n+i)==-1)
			{
				i++;
			//	printf("*\n");
			}
			else
				if(prime(n+i)==f(n+i))
				{	
					printf("%I64d\n",n+i);
			    	break; 
				}
		    	else
				{
					i++;
				//	printf("*\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