Java:打印八位以内的所有水仙花数

public class TestDemo3 {
	public static void main(String[] args) {
		int i;
		for (i=0;i<=99999999;i++) {
			//确定位数
			int count = 0;
			int tmp = i;
			while(tmp != 0) {
				count++;
				tmp = tmp/10;  
			}
			//求数字i的每一位
			int sum = 0;
			tmp = i;
			while(tmp != 0){
				sum += Math.pow(tmp%10,count);
				tmp = tmp/10;
			}
			if(sum==i){
				System.out.println(sum);
			}
		}
	}
}
发布了82 篇原创文章 · 获赞 0 · 访问量 1194

猜你喜欢

转载自blog.csdn.net/yufy0528/article/details/104729756