TopCoder SRM 582 DIV2 250

Problem Statement

 Magical Girl Iris loves perfect squares. A positive integer n is a perfect square if and only if there is a positive integer b >= 1 such that b*b = n. For example, 1 (=1*1), 16 (=4*4), and 169 (=13*13) are perfect squares, while 2, 54, and 48 are not.
Iris also likes semi-squares. A positive integer n is called a semi-square if and only if there are positive integers a >= 1 and b > 1 such that a < b and a*b*b = n. For example, 81 (=1*9*9) and 48 (=3*4*4) are semi-squares, while 24, 63, and 125 are not. (Note that we require that a < b. Even though 24 can be written as 6*2*2, that does not make it a semi-square.)
You are given a int N. Return "Yes" (quotes for clarity) if N is a semi-square number. Otherwise, return "No".

Definition

 
Class:SemiPerfectSquare
Method:check
Parameters:int
Returns:string
Method signature:string check(int N)
(be sure your method is public)
 
 

Notes

-The return value is case-sensitive. Make sure that you return the exact strings "Yes" and "No".

Constraints

-N will be between 2 and 1000, inclusive.

Examples

0) 
 
48
Returns: "Yes"
48 can be expressed as 3 * 4 * 4. Therefore, 48 is a semi-square.
1) 
 
1000
Returns: "No"
1000 can be represented as 10 * 10 * 10, but it doesn't match the definition of semi-perfect squares.
2) 
 
25
Returns: "Yes"
3) 
 
47
Returns: "No"
4) 
 
847
Returns: "Yes"

Code:


#include<string>
#include<iostream>
using namespace std;

class SemiPerfectSquare
{
public:
	string check(int n){
		int i,j,mul;
		string str="No";
		for(i=2;i*i<=n;i++){
			mul=i*i;
			for(j=1;j<i&&mul*j<=n;j++){
				if(n==mul*j){
					str="Yes";
					return str;
				}
			}
		}		
		return str;
	}
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值