• 【TFLSnoi李志帅】第⑦篇文章---两个数的平方和(课堂练习)


    给出一个整数N,将N表示为2个整数i与j的平方之和(i <= j),如果有多种表示,按照i的递增序输出。

    例如:N = 130,130 = 3^2 + 11^2 = 7^2 + 9^2(注:3^2 + 11^2同11^2 + 3^2算1种)

    Input一个数N(1 <= N <= 10^9)Output共K行:每行2个数,i j,表示N = i^2 + j^2(0 <= i <= j)。 如果无法分解为2个数的平方和,则输出No SolutionSample Input

    130

    Sample Output

    3 11
    7 9
    ________________________________________________________________________________________________________________________________________________
    满分代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int main()
     4 {
     5     long long n,ans=1;
     6     cin>>n;
     7     for(int x=0;x<=sqrt(n);x++)//着重注意x枚举的范围
     8     {
     9         int y=sqrt(n-x*x);//由于y是int类型,所以除整除情况以外,y的值都会上下取整而改变
    10         if(y*y+x*x==n && x<=y)//如果y的平方+x的平方正好等于n,说明sqrt(n-x*x)的结果正好是整数,符合题目要求
    11         {
    12             ans=0;//判断旗帜
    13             cout<<x<<" "<<y<<endl;
    14         }
    15     }
    16     if(ans)cout<<"No Solution";
    17     return 0;
    18 }
  • 相关阅读:
    channel分析
    Nginx|基础
    item2
    搜索引擎技巧
    计算机网络|概述
    操作系统|进程
    分布式事务一致性
    画图工具StartUML
    内存分配
    MPG分析
  • 原文地址:https://www.cnblogs.com/TFLSc1908lzs/p/13531202.html
Copyright © 2020-2023  润新知