算法之水仙花数(Java语言)
概述
在数论中,水仙花数(Narcissistic number),也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number) ,用来描述一个N位非负整数,其各位数字的N次方和等于该数本身。
举例
例如153、370、371及407就是三位超完全数字不变数,其各个数之立方和等于该数:
- 153 = 13 + 53 + 33。
- 370 = 33 + 73 + 03。
- 371 = 33 + 73 + 13。
- 407 = 43 + 03 + 73。
Java算法
/**
* A Narcissistic number is a number that is the sum of its own digits each
* raised to the power of the number of digits. E.g., 0, 1, 2, 3, 4, 5, 6, 7, 8,
* 9, 153, 370, 371, 407, 1634, 8208, 9474.
*/
public class NarcissisticNumberExample {
//判断value是否为水仙花数
public static boolean isNarcissisticNumber(int value) {
int temp = value;
int digits = 0;
//判断value有几位数,保存在digits
while (temp > 0) {
digits++;
temp /= 10;
}
temp = value;
int sum = 0;
while (temp > 0) {
sum += Math.pow(temp % 10, digits);
temp /= 10;
}
return sum == value;
} //开始数和结束数
public static void printNarcissistics(int from, int to) {
int which=0;
for (int i = from; i <= to; i++)
if (isNarcissisticNumber(i)){
which++;
System.out.println("第"+which+"个水仙数是:"+i);
} } //1000里有几个水仙数
public static void main(String[] args) {
printNarcissistics(0,1000);
} }
结果
第1个水仙数是:0
第2个水仙数是:1
第3个水仙数是:2
第4个水仙数是:3
第5个水仙数是:4
第6个水仙数是:5
第7个水仙数是:6
第8个水仙数是:7
第9个水仙数是:8
第10个水仙数是:9
第11个水仙数是:153
第12个水仙数是:370
第13个水仙数是:371
第14个水仙数是:407
参考链接:
维基百科:https://zh.wikipedia.org/wiki/%E6%B0%B4%E4%BB%99%E8%8A%B1%E6%95%B0
Java for Beginners-Narcissistic Number: http://primaryjava.blogspot.hk/2013/10/narcissistic-number.html
算法之水仙花数(Java语言)的更多相关文章
- Java实现 蓝桥杯VIP 算法训练 水仙花数
这道题有两个方法,第一个就相对来说通俗易懂 第二个可以用到Java的一些方法 public class 水仙花数1 { public static void main(String[] args) { ...
- 水仙花数 java 实现
题目描述: 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3.现在要求 ...
- 算法之求质数(Java语言)
质数(Prime number) 又称素数,指在的自然数中,除了1和该数自身外,无法被其他自然数整除的数(也可定义为只有1与该数本身两个因数的数). 算法原理 验证一个数字 n 是否为素数的一种简单但 ...
- 《神经网络算法与实现-基于Java语言》的读书笔记
文章提纲 全书总评 读书笔记 C1.初识神经网络 C2.神经网络是如何学习的 C3.有监督学习(运用感知机) C4.无监督学习(自组织映射) Rreferences(参考文献) 全书总评 书本印刷质量 ...
- 51Nod--1015 水仙花数
51Nod: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1015 1015 水仙花数 基准时间限制:1 秒 空间 ...
- php 求水仙花数优化
水仙花数是指一个n位数(n>=3),它每一个位上数字的n次幂之和等于它本身,n为它的位数.(比如:1^3+5^3+3^3 = 153) 水仙花数又称阿姆斯特朗数. 三位的水仙花数有4个:153, ...
- 51Nod 1016 水仙花数 V2(组合数学,枚举打表法)
1016 水仙花数 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 水仙花数是指一个 n 位数 ( n≥3 ) ...
- 51Nod 1001 数组中和等于K的数对 And 1015 水仙花数
1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K ...
- Java语言写出水仙花数,
package com.llh.demo;/** * 水仙花数 * @author llh * */public class Demo14 { public static void main(S ...
随机推荐
- [转] flume使用(六):后台启动及日志查看
[From] https://blog.csdn.net/maoyuanming0806/article/details/80807087 处理的问题flume 普通方式启动会有自己自动停掉的问题,这 ...
- 16个最佳响应式HTML5框架分享
HTML5框架可以快速构建响应式网站,它们帮助程序员减少编码工作,减少冗余的代码.如今有很多免费的HTML5框架可供使用,由于它们有着响应式设计.跨浏览器兼容.相对轻量级等特点,这些框架在开发中都十分 ...
- Sublime 必知必会(持续更新)
1.格式化代码 Edit - Line - Reindent(中文路径则是:编辑 - 行 - 再次缩进) 2.分屏显示 view-layout-Columns:2(中文路径则是:查看 - 布局 - 列 ...
- hibernate的AnnotationHelloWorld
来龙去脉: 最开始sun这个土鳖设计了EJB2.0.EJB2.1那个时代.后来有人发现设计的很烂,不好用,就设计了hibernate,,人们发现用hibernate反而比EJB2.0.2.1好,hib ...
- 浅谈js中的垃圾两种回收机制
一.标记清除 标记清除的主要思想是先建立各个对象的关联,然后从根节点出发,使用广度优先搜索依次标记所有对象,那些不能被标记的对象就应该作为垃圾回收. 这种方式的主要缺点就是如果某些对象被清理后,内存是 ...
- javac之Method Invocation Expressions
15.12.1. Compile-Time Step 1: Determine Class or Interface to Search 15.12.2. Compile-Time Step 2: D ...
- python-Condition 进程同步互斥
#!/usr/bin/python import multiprocessing,time def A(cond): name=multiprocessing.current_process().na ...
- i.mx android6 输入子系统分析(未完)
参考:http://blog.csdn.net/u010312937/article/details/53285286 https://www.jianshu.com/p/7fca94b330ea ...
- i.mx6 Android5.1.1 Zygote
0. 总结: 0.1 相关源码目录: framework/base/cmds/app_process/app_main.cppframeworks/base/core/jni/AndroidRunti ...
- [转]SSRS: Checking for Divide By Zero Using Custom Code
本文转自:http://salvoz.com/blog/2011/11/25/ssrs-checking-for-divide-by-zero-using-custom-code/ I encount ...