zoukankan      html  css  js  c++  java
  • 1080 两个数的平方和

    1080 两个数的平方和

    基准时间限制:1 秒 空间限制:131072 KB
    给出一个整数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
    #include <cmath>
    #include <cstdio>
    bool judge(int x)
    {
        if(sqrt(x)== (double)floor(sqrt(x)))
            return 1;
        return 0;
    }
    int main()
    {
        int n;
        while(scanf("%d", &n) != EOF)
        {
            int mid=ceil(sqrt(ceil(n/2)));
            bool flag=0;
            for(int i=0; i<=mid; i++)
            {
                int bb=n-i*i;
                if(judge(bb))
                {
                    int sb= (int)sqrt(bb);
                    if(sb< i) break;
                    flag=1;
                    if(sb ==i)
                    {
                        printf("%d %d
    ", i, i);
                        break;
                    }
                    printf("%d %d
    ", i, sb);
                }
            }
            if(!flag)
                printf("No Solution
    ");
        }
        return 0;
    }
  • 相关阅读:
    HDU --1251
    POJ -- 2436
    POJ -- 3140
    POJ 3107
    POJ -- 2002
    POJ -- 1655
    lintcode154
    lintcode192
    lintcode582
    lintcode901
  • 原文地址:https://www.cnblogs.com/ceal/p/5469048.html
Copyright © 2011-2022 走看看