hdu2098分拆素数和

http://acm.hdu.edu.cn/showproblem.php?pid=2098

网上另一种代码多的是,有兴趣的自己看看吧,这道题目明显想复杂了,数据不是很大,却用了打表;

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "math.h"
#include "algorithm"
#include "iostream"

using namespace std;
const int N = 1300000;//µÚ10Íò¸öÊýΪ1299709  
int prime[N];  
bool notPrime[N]; 
int count1 ;
void init_prime()
{
	 memset(prime,0,sizeof(prime));  
    memset(notPrime,0,sizeof(notPrime));  
    count1 = 0;  
    for(int i = 2; i < N;i++)//ÇóËØÊý±í  
    {  
        if(!notPrime[i])  
        {  
            prime[count1++] = i;  
        }  
  
        for(int j = 0; j < count1 && (i * prime[j] < N);j++)  
        {  
            notPrime[i * prime[j]] = true;  
            if(!(i % prime[j]))  
                break;  
        }  
    }  
}

int main()
{
	int n ;
	int i , j ;
	init_prime() ;
	while( scanf( "%d" , &n ) != EOF && n)
	{
		int sum = 0 ;
		for( i = 0 ; prime[ i ] <= n ; i++ )
		{
			for( j = i + 1 ; prime[ j ] <= n ; j++ )
			{
				if( prime[ i ] + prime[ j ] == n )
				{
						sum++ ;
						break ;
				}
			}
			//cout << "aaaaaa" <<endl ;
		}
		cout << sum << endl ;
	} 
	return 0;
}


原文地址:https://www.cnblogs.com/javawebsoa/p/3093400.html