给一个整数 c
, 你需要判断是否存在两个整数 a
和 b
使得 a^2 + b^2 = c
.
样例
样例 1:
输入 : n = 5
输出 : true
说明 : 1 * 1 + 2 * 2 = 5
样例 2:
输入 : n = -5
输出 : false
class Solution {
public:
/**
* @param num: the given number
* @return: whether whether there're two integers
*/
bool checkSumOfSquareNumbers(int num) {
// write your code here
if(num < 0)
return false;
if(num == 0)
return true;
for(int i = 0; i <= sqrt(num); i++)
{
int tmp = i * i;
int chazhi = num - tmp;
int tmp1 = sqrt(chazhi);
if((tmp1 * tmp1) == chazhi)
return true;
}
return false;
}
};