633. Sum of Square Numbers

 1 static int wing=[]()
 2 {
 3     std::ios::sync_with_stdio(false);
 4     cin.tie(NULL);
 5     return 0;
 6 }();
 7 
 8 class Solution 
 9 {
10 public:
11     bool judgeSquareSum(int c) 
12     {
13         int a=0,b=sqrt(c);
14         while(a<=b)
15         {
16             int k=a*a+b*b;
17             if(k==c)
18                 return true;
19             else if(k>c)
20                 b--;
21             else
22                 a++;
23         }
24         return false;
25     }
26 };

数学方法,记住即可。

猜你喜欢

转载自www.cnblogs.com/zhuangbijingdeboke/p/9147076.html