小言_互联网的博客

输出所有的水仙花数,把谓水仙花数是指一个数3位数,其各各位数字立方和等于其本身, 例如: 153 = 1*1*1 + 3*3*3 + 5*5*5

293人阅读  评论(0)

package practice;

public class Practice05 {
/*
* 输出所有的水仙花数,把谓水仙花数是指一个数3位数,其各各位数字立方和等于其本身,
例如: 153 = 111 + 333 + 555
/
public static void main(String[] args) {
System.out.println(“水仙花数为:”);
long startTime=System.nanoTime();
for (int i = 0; i < 1000; i++) {
int temp1=i%10;//个位
int temp2=(i/10)%10;//十位
int temp3=i/100;//百位
if (temp1
temp1
temp1+temp2temp2temp2+temp3temp3temp3==i) {
{
System.out.println(i);
}
}

	}

long endTime=System.nanoTime();
System.out.println(“程序运行时间为:”+(endTime-startTime)/1000+“us”);
}
}

package practice01_10;

public class Practice05_Narcissus_Math {
/*
* 输出所有的水仙花数,把谓水仙花数是指一个数3位数,其各各位数字立方和等于其本身,
* 例如: 153 = 111 + 333 + 555
*/
public static void main(String[] args) {
System.out.println(“水仙花数为:”);
long startTime = System.nanoTime();

	for (int i = 100; i < 1000; i++) {
		int tmp1 = i/100;		// 百位数字
		int tmp2 = (i/10)%10;	// 十位数字
		int tmp3 = i%10;		// 个位数字
		//Math.pow(x,y)计算x的y次方
		if ( (Math.pow(tmp1, 3) +  Math.pow(tmp2, 3) + Math.pow(tmp3, 3)) == i ) {
			System.out.println(i);
		}
	}
	long endTime = System.nanoTime();
	System.out.println("程序运行时间:" + (endTime-startTime)/1000 + "us.");
}

}


转载:https://blog.csdn.net/weixin_44083514/article/details/102490009
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场