黑马程序员技术交流社区
标题:
水仙花优化代码(带测试方法)求分
[打印本页]
作者:
刘圣伟
时间:
2012-8-13 18:58
标题:
水仙花优化代码(带测试方法)求分
class shuixian {
public static void main(String args[])
{
long startTime = System.nanoTime();
fa1();
long Time = System.nanoTime() - startTime;
long startTime1 = System.nanoTime();
fa2();
long Time1 = System.nanoTime() - startTime1;
long startTime2 = System.nanoTime();
fa3();
long Time2 = System.nanoTime() - startTime2;
System.out.println("三"+Time2+"\n"+
"一"+Time+"\n"+
"二"+Time1);
}
public static void fa2(){
for(int i=100;i<1000;i++)
{
int a =i%10; //个位
int b = (i/10)%10; //十位
int c =(i/100)%10; //百位
if(Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3)==i)
System.out.println(i);
}
}
public static void fa1(){
for(int i=1;i<10;i++)
{
for(int j=0;j<10;j++)
{
for(int k=0;k<10;k++)
{
if(Math.pow(i,3)+Math.pow(j,3)+Math.pow(k,3)==i*100+j*10+k)
System.out.println(i+""+j+""+k);
}
}
}
}
public static void fa3(){
int n= 100;
for(int i=0;i<900;i++)
{
String ar=(n++)+"";
Integer a = new Integer(ar.substring(0,1));
Integer b = new Integer(ar.substring(1,2));
Integer c = new Integer(ar.substring(2,3));
if(Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3)==a*100+b*10+c)
System.out.println(n);
}
}
}
复制代码
作者:
刘圣伟
时间:
2012-8-13 19:13
劝大家,注意书写的格式,这是对以后发展很重要的。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2