所有四平方定理c++
以下是C++代码实现四平方定理:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int c;
cout << "Enter the value of c: ";
cin >> c;
for (int a = 0; a * a <= c; a++) {
for (int b = a; b * b <= c - a * a; b++) {
for (int d = 0; d * d <= c - a * a - b * b; d++) {
int e = c - a * a - b * b - d * d;
if (e >= 0 && sqrt(e) == (int)sqrt(e)) {
cout << "a = " << a << ", b = " << b << ", c = " << c << ", d = " << d << ", e = " << sqrt(e) << endl;
return 0;
}
}
}
}
cout << "No solution found!" << endl;
return 0;
}
该程序通过三层嵌套循环枚举a、b、d的值,并计算出e的值,判断e是否为完全平方数,如果是,则输出所有变量的值。如果没有找到解,则输出“No solution found!”
原文地址: https://www.cveoy.top/t/topic/fDjm 著作权归作者所有。请勿转载和采集!