c语言中的整数平方和,C ++中的平方和

假设我们有一个非负整数c,我们必须确定是否有两个整数a和b满足a ^ 2 + b ^ 2 = c。

因此,如果输入类似于61,则输出将为True,因为61 = 5 ^ 2 + 6 ^ 2。

为了解决这个问题,我们将遵循以下步骤-定义一个函数isPerfect(),这将需要x,

sr:= x的平方根

当(sr-sr的下限)为0时返回true

从主要方法中执行以下操作:

如果c与0相同,则-返回真

对于初始化i:= 0,当i

b:= c-i * i

如果isPerfect(b)为true,则-

返回假

让我们看下面的实现以更好地理解-#include 

using namespace std;

class Solution {

public:

bool isPerfect(int x){

long double sr = sqrt(x);

return ((sr - floor(sr)) == 0);

}

bool judgeSquareSum(int c) {

if (c == 0)

return true;

int b;

for (int i = 0; i 

b = c - i * i;

if (isPerfect(b))

return true;

}

return false;

}

};

main(){

Solution ob;

cout <

}

输入项61

输出结果1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值