2851: 数字游戏

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 187   Solved: 98
[ Submit][ Status][ Web Board]

Description

输入若干个正整数,将其中能写成其它两个正整数的平方和的数输出来。
例,若输入的数中有5和25,这两个数应该输出,因为5 = 12 + 22,25 = 32 + 42
请在下面的代码基础上完成本题,只提交你编写的部分
#include <iostream>
#include <cmath>
using namespace std;
bool f(int n);
int main( )
{
     int n;
     while(cin>>n&&n>0)
     {
          if(f(n))
               cout<<n<<endl;
     }
     return 0;
}
//下面实现函数f,其功能是判断n是否可以写成其它两个正整数的平方和。
//若n能写成其它两个正整数的平方和,返回true,否则,返回false
//只提交下面的程序段
bool f(int n)
{
      bool result=false;



     return result;
}

Input

若干个正整数,以输入0作为结束标志

Output

输出其中能写成其它两个正整数的平方和的数,一数一行,保持原来的顺序

Sample Input

83 5 12 363 137 307 31 87 126 490 300 358 28 239 286 69 25 94 7 336 0

Sample Output

5
137
490
25

HINT

Source

HLJ

注:博主第一次接触bool.........



代码:

#include <iostream>
 #include <cmath>
 using namespace std;
 bool f(int n);
 int main( )
 {
      int n;
      while(cin>>n&&n>0)
      {
           if(f(n))
                cout<<n<<endl;
      }
      return 0;
 }
bool f(int n)
 {
       bool result=false;
int i,t;
int x;
for(i=1;i<=n/2;i++)
{
for(t=1;t<=i;t++)
{
if(i*i+t*t==n)
return true;
}
}




      return result;
 }

猜你喜欢

转载自blog.csdn.net/qq_41170600/article/details/79832433