(51nod)1080 - 两个数的平方和

    xiaoxiao2021-03-25  75

    1080 两个数的平方和 基准时间限制:1 秒 空间限制:131072 KB 分值: 5  难度:1级算法题  收藏  关注 给出一个整数N,将N表示为2个整数i j的平方和(i <= j),如果有多种表示,按照i的递增序输出。 例如:N = 130,130 = 3^2 + 11^2 = 7^2 + 9^2 (注:3 11同11 3算1种) Input 一个数N(1 <= N <= 10^9) Output 共K行:每行2个数,i j,表示N = i^2 + j^2(0 <= i <= j)。 如果无法分解为2个数的平方和,则输出No Solution Input示例 130 Output示例 3 11 7 9 相关问题 两个数的平方和 V2  640 李陶冶  (题目提供者)

    思路:从两段开始枚举a,b。由于平方的缘故,b由sqrt(n)开始作为较大值。

    #include <cstdio> #include <cmath> #include <algorithm> using namespace std; int main() { int n,a,b; while(~scanf("%d",&n)) { int flag=0; a=0,b=(int)sqrt(n)+1; while(a<=b) { int sum=a*a+b*b; if(sum==n){printf("%d %d\n",a,b);a++;b--;flag=1;} else if(sum>n) b=min(b-1,(int)sqrt(sum-a*a)); else a=max(a+1,(int)sqrt(sum-b*b)); } if(!flag) printf("No Solution\n"); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-33975.html

    最新回复(0)