题目:

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.

Example 1:

Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5

Example 2:

Input: 3
Output: False

分析:

给定一个非负整数c ,你要判断是否存在两个整数a和b,使得 a^2 + b^2 = c。

我们可以给定一个a,来判断c-a*a是不是等于(sqrt(c-a*a))^2,因为一个数如果可以开方,且这个数开方后再平方的话和原来相等就是完全平方数,也就是我们所求的b。

程序:

class Solution {
public:
bool judgeSquareSum(int c) {
for(double a = ; a*a <= c; ++a){
int b = sqrt(c - a*a);
if(b*b == (c - a*a))
return true;
}
return false;
}
};

LeetCode 633. Sum of Square Numbers平方数之和 (C++)的更多相关文章

  1. [LeetCode] 633. Sum of Square Numbers 平方数之和

    Given a non-negative integer c, your task is to decide whether there're two integers a and b such th ...

  2. [LeetCode] Sum of Square Numbers 平方数之和

    Given a non-negative integer c, your task is to decide whether there're two integers a and b such th ...

  3. Leetcode633.Sum of Square Numbers平方数之和

    给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2: 输入: 3 ...

  4. #Leetcode# 633. Sum of Square Numbers

    https://leetcode.com/problems/sum-of-square-numbers/ Given a non-negative integer c, your task is to ...

  5. 【Leetcode_easy】633. Sum of Square Numbers

    problem 633. Sum of Square Numbers 题意: solution1: 可以从c的平方根,注意即使c不是平方数,也会返回一个整型数.然后我们判断如果 i*i 等于c,说明c ...

  6. 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...

  7. 【leetcode】633. Sum of Square Numbers(two-sum 变形)

    Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c. ...

  8. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  9. 633. Sum of Square Numbers【Easy】【双指针-是否存在两个数的平方和等于给定目标值】

    Given a non-negative integer c, your task is to decide whether there're two integers a and bsuch tha ...

随机推荐

  1. October 14th 2017 Week 41st Saturday

    I was well beaten myself, and I am beffer for it. 我自己也被打败过,但我因此变得更好. For most of us, the life road c ...

  2. [技术] OIer的C++标准库 : 字符串库

    引入 上次我在博客里介绍了OI中可能用到的STL中的功能, 今天我们接着来发掘C++标准库中能为OI所用的部分. 点击传送至我的上一篇系列博文 众所周知, OI中经常用到字符串相关的处理, 这时善用字 ...

  3. Alpha事后诸葛亮(阳光普照队)

    Alpha事后诸葛亮 设想和目标 1.实现文字识别,以用户喜欢的图片做背景将其保存,生成新的图片. 2.时间比较赶,主要是因为队员对于Android开发方面的了解不多,可以说是几乎没有,需要一步一步的 ...

  4. 阿里八八Alpha阶段Scrum(1/12)

    任务分配 叶文滔:整体框架UI设计.作为组长进行任务协调 俞鋆:后端服务器及数据库搭建 王国超:日程模块多日显示部分设计 黄梅玲:日程模块单日显示部分设计 林炜鸿:日程模块文本添加部分设计 张岳.刘晓 ...

  5. 转 10 个 Nginx 的安全提示

    Nginx是当今最流行的Web服务器之一.它为世界上7%的web流量提供服务而且正在以惊人的速度增长.它是个让人惊奇的服务器,我愿意部署它. 下面是一个常见安全陷阱和解决方案的列表,它可以辅助来确保你 ...

  6. sql 一个表的字段更新至另一个字段的方法

    update Lc_Taxs set TaxMember = convert(int,Lc_Taxs2.TaxNo)  from Lc_Taxs a,(select * from Lc_Taxs ) ...

  7. 【题解】[HNOI2008]神奇的国度—BZOJ1006。

    之前说顺着打BZOJ结果又被自己给鸽了qwq. ------------------------------------ 言归正传这道题应该怎么做. 先给大家普及一下弦图(连接环上俩个不相邻节点的边称 ...

  8. OpenCV——开操作、闭操作、形态学梯度、顶帽、黑帽

    ---恢复内容开始--- ---恢复内容结束---

  9. 20175310 《Java程序设计》第1周学习总结(2)

    20175310 <Java程序设计>第1周学习总结(2) 教材学习内容总结 本周学习了教材的第一章内容,通过看微课的方式,自主学习,教材上讲的比较简单,主要的问题都在调试代码上,还有一两 ...

  10. DQN(Deep Reiforcement Learning) 发展历程(四)

    目录 不基于模型的控制 选取动作的方法 在策略上的学习(on-policy) 不在策略上的学习(off-policy) 参考 DQN发展历程(一) DQN发展历程(二) DQN发展历程(三) DQN发 ...