1.7 [description of the problem]
The fourth square sum theorem is also known as the Lagrange theorem: every positive integer can be expressed as the sum of squares of at most 4 positive integers. If 0 is included, it can be represented as the sum of squares of 4 numbers. For example, 5 = 0^2 + 0^2 + 1^2 + 2^2 7 = 1^2 + 1^2 + 1^2 + 2^2 (^ symbol means square meaning) for a given positive integer, there may be a variety of square sum representation. Ask you to sort 4 numbers: 0 < = a < = < = < = C < = D, and all possible representations are ascending in a, B, C, D as joint primary keys, and finally output the first representation
[input] a positive integer N (N<5000000)
[output] 4 non negative integers, sorted from small to large, separated by spaces in the middle.
[input case 1] 5
[output example 1] 0012
[input case 2] 773535
[output example 2] 11267838
solved:
#include<stdio.h>
int main(){
int n;
int a=0;
int b=0;
int c=0;
int d=0;
scanf("%d",&n);
for(a;a<=2237;a++){
if(a*a>n) continue;
for(b=a; b<=2237;b++){
if(a*a+b*b>n) break;
for(c=b; c<=2237;c++){
if(a*a+b*b+c*c>n) break;
for(d=c; d<=2237;d++){
if(a*a+b*b+c*c+d*d>n) break;
if(a*a+b*b+c*c+d*d==n) {
printf("%d,%d,%d,%d",a,b,c,d);
return 0;}
}
}
}
}
return 0;
}