本题要求对任意给定的正整数N,求方程X2+Y2=N的全部正整数解。
输入格式:
输入在一行中给出正整数N(≤10000)。
输出格式:
输出方程X2+Y2=N的全部正整数解,其中X≤Y。每组解占1行,两数字间以1空格分隔,按X的递增顺序输出。如果没有解,则输出No Solution。
输入样例1:
884
输出样例1:
10 28
20 22
输入样例2:
11
输出样例2:
No Solution
This type of topic you are looking for solutions of the equation, can be used to compute fast solving the number of characters traverse a given range, my other blog and just the solution of the problem, readers can compare the links are as follows :
添加链接描述
// date:2020/3/6
// author:xiezhg5
#include <stdio.h>
#include <math.h>
int main(void)
{
int n,i,j;
int flag=0; //目标变量
scanf("%d",&n);
for(i=1;i<=sqrt(n)+1;i++) //遍历1到√n找i
{
for(j=1;j<=sqrt(n)+1;j++) //遍历1到√n找j
{
if((i*i+j*j==n)&&(i<=j))
{
printf("%d %d\n",i,j);
flag=1;
break;
}
}
}
if(flag==0) //判断循环条件结束时是否找到i,j的值
{
printf("No Solution\n");
}
return 0;
}