请问怎么编写一个c++程序求所有的三位数,其各位数字的立方和等于该数的本身?

来源:百度知道 编辑:UC知道 时间:2024/06/15 06:13:44
如:153=1×1×1+5×5×5+3×3×3。我刚开始在学C++,我的程序为#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
a=100*b+10*c+1*d;
for( b=1;b<=9;b++)
for( c=1;c<=9;c++)
for( d=1;d<=9;d++)
if(a=b*b*b+c*c*c+d*d*d)
cout<<"所有水仙花数"<<a<<endl;
return 0;

}
可是运算的时候老有警告:warning C4700: local variable 'b' used without having been initialized
warning C4700: local variable 'c' used without having been initialized
warning C4700: local variable 'd' used without having been initialized
而且结果也不是我想要的!哪位高手能帮我这个fresh解决哈!谢谢了!

#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
for( b=1;b<=9;b++)
for( c=0;c<=9;c++)
for( d=0;d<=9;d++)
{
a=100*b+10*c+1*d;
if(a==b*b*b+c*c*c+d*d*d)
cout<<"所有水仙花数"<<a<<endl;
}
return 0;

}
if里面要用“==”

编译后的结果
所有水仙花数153
所有水仙花数370
所有水仙花数371
所有水仙花数407
Press any key to continue

a=100*b+10*c+1*d;
这句之前,B C D都没赋值啊,呵呵

using namespace std;
int main()
{
int a,b,c,d;
for( b=1;b<=9;b++)
for( c=1;c<=9;c++)
for( d=1;d<=9;d++)
{
a=100*b+10*c+1*d;
if(a=b*b*b+c*c*c+d*d*d)
cout<<"所有水仙花数"<<a<<endl;
}
return 0;

}

把a=100*b+10*c+1*d;这句去掉就行了
这句没有起作用,abcd都没赋值