首页 > 试题广场 >

平方和

[编程题]平方和
  • 热度指数:2755 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个正整数 c ,请问是否存在正整数 a , b 满足

数据范围:
示例1

输入

5

输出

true

说明

2^2+1^2=5  
示例2

输入

25

输出

true

说明

4^2+3^2=25  
示例3

输入

24

输出

false
class Solution:
    def square(self , c: int) -> bool:
       
        temp=int(c**0.5)+2
        m=0
        for i in range(1,temp):
            if i*i>=c:
                break
               
            m=(c-i*i)**0.5

            if m%1==0:
                return True
           

        return False
发表于 2024-09-21 14:03:35 回复(0)
1
2
3
4
5
6
7
8
9
10
11
class Solution:
    def square(self , c: int) -> bool:
        # write code here
        if c==1:
            return False
        for a in range(1,int(c**0.5)+1):
            b = c - a**2
            # 检查b是否为平方数
            if b**0.5 == int(b**0.5):
                return True
        return False

发表于 2023-03-27 21:05:32 回复(0)