一开始提交了这个,果断TLE
1 #include <cstdio> 2 #include <iostream> 3 #include <string> 4 #include <queue> 5 #include <vector> 6 #include <map> 7 #include <cmath> 8 using namespace std; 9 10 #define MEM(a,v) memset (a,v,sizeof(a)) 11 // a for address, v for value 12 13 #define max(x,y) ((x)>(y)?(x):(y)) 14 #define max(x,y) ((x)>(y)?(x):(y)) 15 16 #define debug printf("!\n") 17 18 19 bool isPrime(int n) 20 { 21 int i; 22 for(i = 3;i<=sqrtf(n);i++) 23 { 24 if(n%i == 0) 25 return false; 26 } 27 return true; 28 } 29 30 31 int main() 32 { 33 int n; 34 int i,j; 35 int count; 36 while(scanf("%d",&n)!=EOF && n) 37 { 38 count = 0; 39 for(i = 3;i<=n/2;i+=2) 40 { 41 if(isPrime(i)) 42 if(isPrime(n-i)) 43 count++; 44 } 45 printf("%d\n",count); 46 } 47 48 return 0; 49 }
看来只能打表了
1 #include <cstdio> 2 #include <iostream> 3 #include <string> 4 #include <queue> 5 #include <vector> 6 #include <map> 7 #include <cmath> 8 using namespace std; 9 10 #define MEM(a,v) memset (a,v,sizeof(a)) 11 // a for address, v for value 12 13 #define max(x,y) ((x)>(y)?(x):(y)) 14 #define max(x,y) ((x)>(y)?(x):(y)) 15 16 #define debug printf("!\n") 17 18 const int MM = 32769; 19 20 bool isPrime[MM] = {false}; 21 22 void initPrime() 23 { 24 MEM(isPrime,true); 25 int i,j; 26 27 for(i = 1;i<=MM-1;i++) if(i%2==0) isPrime[i]=false; 28 for(i = 3;i<=MM/2+1;i++) 29 { 30 for(j = i+i;j<=MM-1;j+=i) 31 isPrime[j] = false; 32 } 33 isPrime[1] = false; 34 isPrime[2] = true; 35 } 36 37 38 int main() 39 { 40 41 int i,n; 42 initPrime(); 43 while(scanf("%d",&n)!=EOF && n) 44 { 45 int count = 0; 46 for(i = 2;i<=n/2;i++) 47 if(isPrime[i] && isPrime[n-i]) 48 count++; 49 printf("%d\n",count); 50 } 51 52 return 0; 53 }
421MS
时间: 2024-10-06 11:03:53