1812:完美立方
作者:互联网
描述
形如a3= b3 + c3 + d3的等式被称为完美立方等式。例如123= 63 + 83 + 103 。编写一个程序,对任给的正整数N (N≤100),寻找所有的四元组(a, b, c, d),使得a3 = b3 + c3 + d3,其中a,b,c,d 大于 1, 小于等于N,且b<=c<=d。
输入
一个正整数N (N≤100)。
输出
每行输出一个完美立方。输出格式为:
Cube = a, Triple = (b,c,d)
其中a,b,c,d所在位置分别用实际求出四元组值代入。
请按照a的值,从小到大依次输出。当两个完美立方等式中a的值相同,则b值小的优先输出、仍相同则c值小的优先输出、再相同则d值小的先输出。
样例输入
24
样例输出
Cube = 6, Triple = (3,4,5)
Cube = 12, Triple = (6,8,10)
Cube = 18, Triple = (2,12,16)
Cube = 18, Triple = (9,12,15)
Cube = 19, Triple = (3,10,18)
Cube = 20, Triple = (7,14,17)
Cube = 24, Triple = (12,16,20)
解题
暴力求解即可
代码
#include <iostream>
using namespace std;
int all[20];
int main() {
int n;
cin >> n;
for (int i = 2; i <= n; i++) {
for (int j = 2; j <= n; j++) {
for (int k = j; k <= n; k++) {
for (int l = k; l <= n; l++) {
if (i * i * i == j * j * j + k * k * k + l * l * l) {
cout << "Cube = " << i << ", Triple = (" << j << ","
<< k << "," << l << ")" << endl;
}
}
}
}
}
}
标签:输出,Cube,1812,完美,int,12,Triple,立方 来源: https://blog.csdn.net/xcdq_aaa/article/details/112630852