枚举:基于逐个尝试答案的一种求解策略 例题1:完美立方 描述: 形如(a^3) = (b^3) +(c^3) +(d^3) 等式称为完美立方等式。例如12,6,8,10。编写一个程序,对任给的正整数N(N<=100),寻找所有的四元组(a,b,c,d),使得满足(a^3) = (b^3) +(c^3) +(d^3) ,其中a,b,c,d大于1,小于等于N,且b<=c<=d。 输入: 一个正整数N(N<=100) 输出: 每行输出一个完美立方。 输入样例: 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) 解题思路: 四重循环枚举a,b,c,d,a在最外层,d在最里层,每一层都是从小到大枚举,范围:a[2,N],b[2,a-1],c[b,a-1],d[c,a-1] 代码: # include <iostream> # include <cstdio> //通过使用c标准输入和输出库