给出一个整数N,将N表示为2个整数i j的平方和(i <= j),如果有多种表示,按照i的递增序输出。
如果无法分解为2个数的平方和,则输出No Solution Sample Input
例如: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 Sample Input
130Sample Output
3 11 7 9
代码如下:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int a[100001];
int l;
int cmp(int x)
{
for(int i=0;i<l;i++)
{
if(x==a[i])
{
return false;
}
}
return true;
}
int main()
{
memset(a,0,sizeof(a));
int n;
scanf("%d",&n);
int x,y;
l=0;
int m = (int)sqrt((double)n);
int flag=1;
for(int i=0;i<=m;i++)
{
x = i;
y = (int)sqrt((double)(n-x*x));
if(x*x+y*y==n)
{
a[l] = y;
bool k =cmp(x);
l++;
if(k)
{
printf("%d %d\n",x,y);
flag =0;
}else{
break;
}
}
}
if(flag==1) printf("No Solution\n");
return 0;
}