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 dzs8819 at 2007-05-12 17:32:06 on Problem 2262
#include<stdio.h>
#include<math.h>
int cnt=1;
long a[100000];
int prime(long n);
int main()
{
    long n,i;
    while(scanf("%ld",&n) && n!=0)
    {
        for(i=2;i<n;i++)
            if(prime(i))
            {
                a[cnt]=i;
                cnt++;
                if(prime(n-i))
                {
                    a[cnt]=n-i;
                    cnt++;
                    break;
                }
            }
        if(i==n)
            printf("Goldbach's conjecture is wrong\n");
        else
            printf("%ld = %ld + %ld\n",n,i,n-i);
    }
    return 0;
}
int prime(long n)
{
    long i,k=0;
    for(i=1;i<cnt;i++)
    {
        if(n==a[i])
            return 1;
    }
    for(i=2;i<=sqrt(n);i++)
    {
        if(n%i==0)
            k++;
    }
    return (k?0:1);
}

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