这个C++程序为什么没有输出

#include<iostream>
using namespace std;

double power(double x,int n);

int main()
{
int a;
cin>>a;
int b=2;
for(int b=2;b<=a;b++)
{
int bcubed;
bcubed=power(b,3);
for(int c=2;c>b;c++)
{
int ccubed=power(c,3);
for(int d=2;d>b;d++)
{
int dcubed=power(d,3);
for(int e;e>b;e++)
{
int ecubed=power(e,3);
int addtion=ccubed+dcubed+ecubed;
if(addtion==bcubed)
cout<<"Cube="<<b<<","<<"Triple=("<<c<<","<<d<<","<<e<<")"<<endl;
}
}
}
}
return 0;
}
double power(double x, int n)
{
n=3;
double result(1.0);
for(int i=1;i<=n;i++)
result*=x;
return result;
}

没有输出的原因有好几个:
1)int b=2; for(int b=2;b<=a;b++) 在VC6.0 中会报重复定义。
2)e 没有初始化,以后也没有赋值。
3)循环中的条件错误,应该是c<b, d<b, e<b 。
另外,你的代码写的也不规范,我把他从写了一下,你将 a 的值输为100就能看到输出了。
#include<iostream>

int main(void)
{
using namespace std;
double power(double x, int n);

int a;
cin>>a;
int b, c, d, e;
int bcubed, ccubed, dcubed, ecubed;
b = c = d = e = 2;
for(b=2; b<=a; b++)
{
bcubed = power(b, 3);
for (c=2; c<b; c++)
{
ccubed = power(c, 3);
for (d=2; d<b; d++)
{
dcubed = power(d, 3);
for (e=2; e<b; e++)
{
ecubed = power(e, 3);
if (ccubed + dcubed + ecubed == bcubed)
cout<<"Cube="<<b<<","<<"Triple=("<<c<<","<<d<<","<<e<<")"<<endl;
}
}
}
}

return 0;
}

double power(double x, int n)
{
double result = 1.0;

for(int i=0; i<n; i++)
result *= x;

return result;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-02-04
if(addtion==bcubed) 不成立,所以不执行输出
想一想:
在for循环的条件里 c>b,d>b,e>b
b^3=c^3+d^3+c^3这个式子怎么可能成立?

应该是c<b,d<b,e<b 吧
第2个回答  2012-02-04
for(int b=2;b<=a;b++)
{
int bcubed;
bcubed=power(b,3);
for(int c=2;c>b;c++)

这里b最小就是2了,c刚开始也是毛球,c>b永远不可能满足呃,自然无法运行到输出的地方了。
第3个回答  2012-02-06
程序错误:1. int b=2;
for(int b=2;b<=a;b++) //去掉for中的int,否则b重复定义
修改完错误1,程序可以运行,但是运行结果错误
程序错误:2. double power(double x, int n)
{
n=3; //你这一句多余的话,使得函数传入的n成为无用的东西,每次n都是3还传个P啊
double result(1.0);
for(int i=1;i<=n;i++)
result*=x;//result*??这是什么,实数赋值还搞什么
return result;
}
修改到这儿,我对楼主已经佩服的不行了,建议你把这个彻底删除,重新写,完全没有必要改,重新写绝对比改这个轻松
第4个回答  2012-02-04
你的e没有初始化啊
第5个回答  2012-02-04
你是指不能运行,程序有错误,还是指没有出现输出框?
相似回答