给出一个整数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种)
收起
输入
一个数N(1 <= N <= 10^9)
输出
共K行:每行2个数,i j,表示N = i^2 + j^2(0 <= i <= j)。 如果无法分解为2个数的平方和,则输出No Solution
输入样例
130
输出样例
3 11 7 9
这一题主要就是遍历的范围是多少,大多数是遍历为根号下N,也可以用其他的骚操作,这里就分分享一个骚操作。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int N;
while (cin >> N)
{
int A;
double B;
double flag = N;
bool tag = true;
if (N == 1)
{
cout << "0 1\n";
tag = false;
}
for (A = 1; A < flag; A++)
{
B = sqrt(N - A * A);
flag = B;
if (B == (int)B)
{
cout << A << ' ' << B << '\n';
tag = false;
}
}
if (tag)
{
cout << "No Solution\n";
}
}
return 0;
}
原文链接:https://blog.csdn.net/f_zyj/article/details/52072872