打印出所有的 "水仙花数 "

类中包含:
(1)水仙花数的两种求解方法;
(2)求取int值的位数;
(3)求取int值各个位数上的取值;

package test;

import org.junit.Test;

/**
 * 
* @ClassName: NarcissusNumber 
* @Description: TODO
* 打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身:
* 如153是一个水仙花数,153=1的三次方+5的三次方+3的三次方
* 
* 水仙花数(Narcissistic number)也被称为超完全数字不变数
* (pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),
* 水仙花数是指一个 n 位数(n≥3),它的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。
* @author chenliming
* @date 2017年11月3日 上午9:15:28 
*
 */
public class NarcissusNumber {
    /**
     * 
    * @Title: narcissusNumber1
    * @Description: TODO(打印出三位数的水仙花数)
    * @return void    返回类型
     */
    public static void narcissusNumber1() {
        for(int i = 1; i <= 9; i++) {
            for(int j = 0; j <= 9; j++) {
                for(int k = 0; k <= 9; k++) {
                    int result = (int) (Math.pow(i, 3) + Math.pow(j, 3) + Math.pow(k, 3));
                    int narcissusNumber = Integer.parseInt(i + "" + j + "" + k);
                    if(narcissusNumber == result) {
                        System.out.println(narcissusNumber);
                    }
                }
            }
        }
    }

    /**
     * 
    * @Title: narcissusNumber2
    * @Description: TODO(打印出某个范围的水仙花数)
    * 
    * 分析:
    *   (1)求出当前数的位数;
    *   (2)获取每个位数上的值,按照水仙花数规范进行计算,结果比对;
    * @return void    返回类型
    * @param start
    * @param end
     */
    public static void narcissusNumber2(int start, int end) {
        for(int i = start; i <= end; i++) {
            int currentNum = i;
            //求出当前数值的位数
            int numAbs = Math.abs(currentNum);
            int count = 0;
            while(numAbs > 0) {
                count++;
                numAbs /= 10;
            }

            //获取每一位上的数值进行水仙花运算
            int totalResult = 0;
            for (int j = 1; j <= count; j++) {
                int hundreds = (int) Math.pow(10, j - 1);
                int result = (currentNum / hundreds) % 10;

                int pow = (int) Math.pow(result, count);
                totalResult += pow;
            }

            if(totalResult == currentNum) {
                System.out.println(currentNum);
            }
        }
    }


    /**
     * 
    * @Title: count
    * @Description: TODO(求int值为几位数)
    * 对数值不断取取10的商值知道商值小于0
    * 
    * @return int    返回类型
    * @param num
    * @return
     */
    public static int count(int num) {
        int numAbs = Math.abs(num);
        int total = 0;
        while(numAbs > 0) {
            total++;
            numAbs /= 10;
        }
        return total;
    }

    /**
     * 
    * @Title: getEveryNum
    * @Description: TODO(打印出int值每一位上的数值)
    * 
    * 分析:
    *   9867
    *   个位: 9867/10^0%10
    *   十位: 9867/10^1%10
    *   百位: 9867/10^2%10
    *   千位: 9867/10^3%10
    *   
    *   求取某一位上的数值,是先对当前位取商值,之后对10进行取余数
    * 
    * @return void    返回类型
    * @param num
     */


    @Test
    public static void getEveryNum(int num) {
        int count = count(num);
        for (int i = 1; i <= count; i++) {
            int hundreds = (int) Math.pow(10, i - 1);
            int result = (num / hundreds) % 10;
            System.out.println(hundreds + "位数:" + result);
        }
    }
    public static void main(String[] args) {
        narcissusNumber1();
        narcissusNumber2(100, 999);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值