【问题描述】
第四平方和定理,又称为拉格朗日定理:每个正整数都可以表示为至多4个正整数的平方和。 如果把0包括进去,就正好可以表示为4个数的平方和。比如:5 = 0^2 + 0^2 + 1^2 + 2^2 7 = 1^2 + 1^2 + 1^2 + 2^2 (^符号表示乘方的意思)对于一个给定的正整数,可能存在多种平方和的表示法。 要求你对4个数排序: 0 <= a <= b <= c <= d 并对所有的可能表示法按 a,b,c,d 为联合主键升序排列,最后输出第一个表示法
【输入】 一个正整数N (N<5000000)
【输出】 4个非负整数,按从小到大排序,中间用空格分开
【输入范例1】 5
【输出范例1】0 0 1 2
【输入范例2】773535
第四平方和定理,又称为拉格朗日定理:每个正整数都可以表示为至多4个正整数的平方和。 如果把0包括进去,就正好可以表示为4个数的平方和。比如:5 = 0^2 + 0^2 + 1^2 + 2^2 7 = 1^2 + 1^2 + 1^2 + 2^2 (^符号表示乘方的意思)对于一个给定的正整数,可能存在多种平方和的表示法。 要求你对4个数排序: 0 <= a <= b <= c <= d 并对所有的可能表示法按 a,b,c,d 为联合主键升序排列,最后输出第一个表示法
【输入】 一个正整数N (N<5000000)
【输出】 4个非负整数,按从小到大排序,中间用空格分开
【输入范例1】 5
【输出范例1】0 0 1 2
【输入范例2】773535
【输出范例2】1 1 267 838
C于语言代码
#include<stdio.h> #include<math.h> int main() { int a, b, c, N, flag=0; double n, d; scanf("%d", &N); n=sqrt(N); while (N>=5000000){ printf("ERROR\n"); scanf("%d", &N);} for(a=0; a<=n; a++){ for(b=a; b<=n; b++){ for(c=b; c<=n; c++){ d=sqrt(N-a*a-b*b-c*c); if(d==(int)d) { printf("%d %d %d %d\n", a, b, c, (int)d); flag=1; break; } }if(flag) break; } if(flag) break; } return 0; }