日期:2014-05-20  浏览次数:20834 次

求救:如何找出所有三位数中个、十、百位数的立方和等于该数本身的三位数?
看看我这个问题出在哪:

public   class   A_3   {
public   static   void   main(String[]   args){
int   a   =0,b   =0,c   =0;
if((a> 0&&a <10)||(b> 0&&b <10)||(c> 0&&c <10)){
int   math   =   a*100   +   b*10   +   c;
int   lfhe   =   a*a*a   +b*b*b   +c*c*c;
for(math   =   100;math <1000;math++){
if(math   ==   lfhe)
System.out.println(math);
}
}
}
}

------解决方案--------------------
你没有将3位数的百位,十位,个位 给 分离出来放到 a,b,c里。我给你个例子

public class A_3 {


public static void main(String[] args){

int a = 0,b = 0,c = 0;
System.out.println( "水仙花数: ");
for(int i=100;i <1000;i++)
{
a = i/100; // 百位
b = i%100/10; // 十位
c = i%100%10; // 个位
if(i==a*a*a+b*b*b+c*c*c)
{
System.out.print(i+ "\t ");
}
}

}

}
------解决方案--------------------
public class Test
{
static int a,b,c,d;
public static void main(String [] args)
{

for(int j=100;j <=999;j++)
{
p(j);
if(Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3)==j)
System.out.println(j);
}
}
public static void p(int i)
{
a = i/100;
d = i%100;
b = d/10;
c = d%10;
}
}

------解决方案--------------------
楼主这是我写的,先取得个、十、百位,再去拼三位数,然后求结果。
public class A_3 {
public static void main(String[] args){
int a,b,c;
for(a=1;a <=9;a++)//百位不能为0
{
for(b=0;b <=9;b++)
{
for(c=0;c <=9;c++)
{
int math = a*100 + b*10 + c;
int lfhe = a*a*a +b*b*b +c*c*c;
if(math == lfhe)
System.out.println(math);
}
}
}

}
}