本文共 617 字,大约阅读时间需要 2 分钟。
求xxxx数(独身数、水仙花数、九九重阳数等)
1、求得当前数字是几? 2、求得每位数字是多少 3、加起来是否=原来数字**public class TestDemo2{ public static void main(String[] args) { //求xxxx数(独身数、水仙花数、九九重阳数等) //1、求得当前数字是几? //2、求得每位数字是多少 //3、加起来是否=原来数字 for (int i = 0;i <= 999; i++ ) { //1、判断当前i是几位数 //总结:用除法除10就可以判断当前数字是几位数 int temp = i;//保存i的数 int count = 0;//判断是几位数 while(temp != 0) { count++; temp = temp/10; } temp = i; int sum = 0; //得到各位的值————》%10/10 while(temp != 0) { sum += Math.pow(temp%10, count); temp = temp/10; } if(sum == i) { System.out.println("xxxxxx数字为:" + sum); } } }**
转载地址:http://pbsgz.baihongyu.com/