zoukankan      html  css  js  c++  java
  • 51 nod 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

    题解:水题
     1 #include <iostream>
     2 #include <cmath>
     3 using namespace std;
     4 int n;
     5 int main()
     6 {
     7     cin>>n;
     8     int j=(int)sqrt(double(n))+1;
     9     int i=0;
    10     int flag=0;
    11     while(i<=j)
    12     {
    13         if(i*i+j*j>n)
    14             j--;
    15         else if(i*i+j*j<n)
    16             i++;
    17         else if(i*i+j*j==n)
    18         {
    19             flag=1;
    20             cout<<i<<" "<<j<<endl;
    21             i++;
    22             j--;
    23         }
    24 
    25     }
    26     if(!flag)
    27         cout<<"No Solution"<<endl;
    28     return 0;
    29 }
    View Code
     
  • 相关阅读:
    Day10
    Day9
    Day8
    Day7
    Day 6
    Day5
    第一周计划
    事件总线模式辨析
    解释器模式辨析
    解释器模式深度探究
  • 原文地址:https://www.cnblogs.com/onlyli/p/7252545.html
Copyright © 2011-2022 走看看