zoukankan      html  css  js  c++  java
  • YTU 2851: 数字游戏

    2851: 数字游戏

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 164  解决: 85

    题目描述

    输入若干个正整数,将其中能写成其它两个正整数的平方和的数输出来。
    例,若输入的数中有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;
    }

    输入

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

    输出

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

    样例输入

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

    样例输出

    5
    137
    490
    25

    你  离  开  了  ,  我  的  世  界  里  只  剩  下  雨  。  。  。

    #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,j;
        for(i=1; i*i<=n; i++)
            for(j=1; j*j<=n; j++)
                if(i*i+j*j==n)result=true;
        return result;
    }
    

  • 相关阅读:
    LeetCode 226. Invert Binary Tree
    LeetCode 221. Maximal Square
    LeetCode 217. Contains Duplicate
    LeetCode 206. Reverse Linked List
    LeetCode 213. House Robber II
    LeetCode 198. House Robber
    LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)
    LeetCode 171. Excel Sheet Column Number
    LeetCode 169. Majority Element
    运维工程师常见面试题
  • 原文地址:https://www.cnblogs.com/im0qianqian/p/5989625.html
Copyright © 2011-2022 走看看