import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a,b,c,d;
int sqrt=(int) Math.sqrt(n);
for(a=0;a<=sqrt;a++)
for (b = a; b<=sqrt; b++)
for (c= 0; c<=sqrt; c++)
for (d= 0; d<=sqrt; d++)
if(n==a*a+b*b+c*c+d*d)
{
if(c>d)
{
int temp=d;
d=c;
c=temp;
}
System.out.print(a+" "+b+" "+c+" "+d);
return;
}
}
}
7
1 1 1 2
本文介绍了一种用于查找四个整数的平方和等于给定整数n的算法。通过使用Scanner类读取输入,算法首先计算n的平方根,然后通过四层循环遍历可能的整数组合,直到找到满足条件的四个整数。找到解决方案后,算法将这四个整数按降序排列并打印。
2761

被折叠的 条评论
为什么被折叠?



