“四方定理”是数论中著名的一个定理,指所有自然数至多只要用四个数的平方和就可以表示。
#include <iostream>
using namespace std;
int main() {
long n, a, b, c, d;
cout << "请输入一个数:" << endl;
cin >> n;
for (a = 1; a <= n; a++) {
for (b = 1; b <= a; b++) {
for (c = 1; c <= b; c++) {
for (d = 1; d <= c; d++) {
if ((a * a + b * b + c * c + d * d) == n) {
cout << a << "*" << a << "+" << b << "*" << b << "+" << c << "*" << c << "+" << d << "*" << d << "=" << n <<endl;
break;
}
}
}
}
}
}