poj2262

简单题

View Code
   
     
#include < iostream >
#include
< cstdio >
#include
< cstdlib >
#include
< cstring >
#include
< cmath >
using namespace std;

#define N 1000005

bool is [N];
int prm[N];

int getprm( int n)
{
int i, j, k = 0 ;
int s, e = ( int )(sqrt( 0.0 + n) + 1 );
memset(
is , 1 , sizeof ( is ));
prm[k
++ ] = 2 ; is [ 0 ] = is [ 1 ] = 0 ;
for (i = 4 ; i < n; i += 2 ) is [i] = 0 ;
for (i = 3 ; i < e; i += 2 )
if ( is [i])
{
prm[k
++ ] = i;
for (s = i * 2 , j = i * i; j < n; j += s)
is [j] = 0 ;
}
for (; i < n; i += 2 )
if ( is [i])prm[k ++ ] = i;
return k;
}

int main()
{
// freopen("t.txt", "r", stdin);
int tot = getprm( 1000000 );
int n;
while (scanf( " %d " , & n), n)
{
for ( int i = 0 ; i < tot && prm[i] * 2 <= n; i ++ )
if ( is [n - prm[i]])
{
printf(
" %d = %d + %d\n " , n, prm[i], n - prm[i]);
break ;
}
}
return 0 ;
}

你可能感兴趣的:(poj)