Prime Cuts

http://poj.org/problem?id=1595

基础不过关,做了两个小时;

// File Name: poj1595.cpp
// Author: bo_jwolf
// Created Time: 2013年10月11日 星期五 16:29:12

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
vector prime;
const int maxn = 10005;
bool unprime[ maxn ];
int num[ maxn ];
void Union(){
	memset( unprime, 0, sizeof( unprime ) );
	unprime[ 0 ] = unprime[ 1 ] = true;
	prime.push_back( 0 );
	prime.push_back( 1 );
	for( int i = 2; i < maxn; ++i ){
		if( !unprime[ i ] ){
			prime.push_back( i );
			for( int j = i + i; j < maxn; j += i ){
				unprime[ j ] = 1;
			}
		}
	}
}

int main(){
	Union();
	int k, n, m, temp1, temp2;
	//for( int i = 1; i < 10; ++i )
	//	cout << prime[ i ] << endl;
	while( cin >> n >> m ){
	//	cout << n << " " << m << ":";
		k = 0;
		int i;
		for( i = 1; n >= prime[ i ]; ++i )
			k = i;
		//cout << k << endl;
		cout << n << " "  << m << ":";
		bool flag = 1;
		if( 2 * m >= k )
			flag = 1;
			//cout << 1 << endl;
		else
			flag  = 0;
		//cout << flag << endl;
		//	cout << 0 << endl;*/
		if( flag ){
			for( int i = 1; i <= k; ++i ){
				cout << " " << prime[ i ];
			}
		}
		else{
				if( k % 2 == 0 ){
					temp1 = k / 2 - m + 1;
					temp2 = k / 2 + m;
				}
				else{
						temp1 = ( k + 1 ) / 2 - m + 1;
						temp2 = k / 2 + m;
				}
				for( int i = temp1; i <= temp2; ++i )
					cout << " " << prime[ i ];
		}
		cout << endl << endl;
	}
	return 0;
}


你可能感兴趣的:(模拟,水题,Poj,素数性质)