http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1080&judgeId=588508
基准时间限制: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^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 Solution
Input示例
130
Output示例
3 11
7 9
代码:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
cin>>n;
int j;
int flag=0;
int vis[100005]={0};
for(int i=0;i<=sqrt(n);i++)
{
j=n-i*i;
if(vis[i])
break;
if((int)sqrt(j)==(double)sqrt(j))
{
cout<<i<<" "<<sqrt(j)<<endl;
j=sqrt(j);
vis[j]=1;
flag=1;
}
}
if(flag==0)
cout<<"No Solution"<<endl;
return 0;
}