#4392: 要如何寫才能不TLE呢?我已經試好幾次了!


u09800103 (Rah-rah-ah-ah-ah !)

學校 : 臺北市立麗山高級中學
編號 : 12642
來源 : [118.168.248.52]
最後登入時間 :
2014-02-19 12:45:21
c050. 00453 - Goldbach's Conjecture -- UVa543 | From: [218.167.97.209] | 發表日期 : 2010-10-14 23:26

#include<stdio.h>
#include<math.h>
int cc(long int a);
int main(void){
        long int a;
        while(scanf("%ld",&a)!=EOF){
            if(a==0)break;
            else if(a%2==1)
                printf("Goldbach's conjecture is wrong.\n");
            else
                for(long int i=3;i<a/2+1;i+=2)
                    if(cc(i)==0&&cc(a-i)==0){
                        printf("%ld = %ld + %ld\n",a,i,a-i);
                        break;
                    }
        }
        return 0;
}
int cc(long int a){
        for(long int i=3;i<=pow(a,1/2.0);i+=2)
            if(a%i==0)return 1;
        return 0;
}
希望厲害的高手們能幫忙看看 
#4402: Re:要如何寫才能不TLE呢?我已經試好幾次了!


leopan0922 (zz)

學校 : 臺北市立成功高級中學
編號 : 6612
來源 : [140.113.225.106]
最後登入時間 :
2016-08-15 15:44:07
c050. 00453 - Goldbach's Conjecture -- UVa543 | From: [219.70.171.51] | 發表日期 : 2010-10-16 22:27

#include

#include
int cc(long int a);
int main(void){
        long int a;
        while(scanf("%ld",&a)!=EOF){
            if(a==0)break;
            else if(a%2==1)
                printf("Goldbach's conjecture is wrong.\n");
            else
                for(long int i=3;i                    if(cc(i)==0&&cc(a-i)==0){
                        printf("%ld = %ld + %ld\n",a,i,a-i);
                        break;
                    }
        }
        return 0;
}
int cc(long int a){
        for(long int i=3;i<=pow(a,1/2.0);i+=2)
            if(a%i==0)return 1;
        return 0;
}
希望厲害的高手們能幫忙看看

 

建個質數表就能過了...

 
#4893: Re:要如何寫才能不TLE呢?我已經試好幾次了!


stanley17112000 (Stanley)

學校 : 國立交通大學
編號 : 13580
來源 : [66.253.158.102]
最後登入時間 :
2019-02-16 03:29:47
c050. 00453 - Goldbach's Conjecture -- UVa543 | From: [211.74.219.196] | 發表日期 : 2011-02-20 23:31

#include

#include
int cc(long int a);
int main(void){
        long int a;
        while(scanf("%ld",&a)!=EOF){
            if(a==0)break;
            else if(a%2==1)
                printf("Goldbach's conjecture is wrong.\n");
            else
                for(long int i=3;i                    if(cc(i)==0&&cc(a-i)==0){
                        printf("%ld = %ld + %ld\n",a,i,a-i);
                        break;
                    }
        }
        return 0;
}
int cc(long int a){
        for(long int i=3;i<=pow(a,1/2.0);i+=2)
            if(a%i==0)return 1;
        return 0;
}
希望厲害的高手們能幫忙看看

 

建個質數表就能過了...

建質數表+二分搜尋數
 
ZeroJudge Forum