这道题是一个枚举问题,主要是循环层数先后的问题,这个基本可以试出来,嵌套for循环的顺序自认为是接触C语言之后遇到的第一个难点,在做过很多题后已经熟练了一些了
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <cstring>
#include <cmath>
#include <cstdlib>
/*例题1 完美立方*/
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
//合理设计枚举的顺序,以提高算法效率,满足题目需求
int main(int argc, char** argv) {
int n;
scanf("%d",&n);
int i,j,k,l;
for(i=2;i<=n;i++)
{
// for(l=i-1;l>1;l--)
for(l=2;l<i;l++)
{
// for(k=l;k>1;k--)
for(k=l;k<i;k++)
{
// for(j=k;j>1;j--)
for(j=k;j<i;j++)
{
if(i*i*i==j*j*j+k*k*k+l*l*l)
{
printf("Cube = %d, Triple = (%d,%d,%d)\n",i,l,k,j);
}
}
}
}
}
//system("pause");
return 0;
}