您的位置:首页 > 其它

【SGU】113. Nearly prime numbers 合数分解

2014-11-06 11:29 357 查看
传送门:【SGU】113. Nearly prime numbers

题目分析:O(sqrt(N))。。

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std ;

#define rep( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )

const int MAXN = 5 ;

int n ;

bool check ( int x ) {
	int top = 0 ;
	for ( int i = 2 ; i * i <= x ; ++ i ) {
		if ( x % i == 0 ) {
			while ( x % i == 0 ) {
				x /= i ;
				top ++ ;
				if ( top > 2 ) return 0 ;
			}
		}
	}
	if ( x > 1 ) ++ top ;
	return top == 2 ;
}

void solve () {
	int x ;
	For ( i , 1 , n ) {
		scanf ( "%d" , &x ) ;
		printf ( check ( x ) ? "Yes\n" : "No\n" ) ;
	}
}

int main () {
	while ( ~scanf ( "%d" , &n ) ) solve () ;
	return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: