水仙花数

统计水仙花数有多少个?

风格不统一 提交于 2020-01-27 09:38:14
水仙花数: 所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。 举例:153就是一个水仙花数。 153 =1x1x1+5x5x5+3x3x3 分析: 0)定义一个统计变量 int count = 0 ; 1)水仙花:就是三位数 ---->for循环 循环中的变量为x 100 ~999 特点: 每个位的数据的立方和是当前数据本身 153 2)获取每个位上的数据 int ge= x % 10 ; int shi = x /10 % 10 ; int bai = x /10 /10 % 10 ; 3)每个位上的数据满足条件 x == ge*ge*ge+shi*shi*shi+bai*bai*bai 4)满足上面的条件 :count++ 5)for循环的输出count值 //定义统计变量 int count = 0 ; //for循环 for ( int x = 100 ; x < 1000 ; x ++ ) { //获取每个位上的数据; int ge = x % 10 ; int shi = x / 10 % 10 ; int bai = x / 10 / 10 % 10 ; //满足条件 if ( x == ( ge * ge * ge + shi * shi * shi + bai * bai * bai ) ) { //输出水仙花数 System . out .

水仙花数(C++)

喜你入骨 提交于 2020-01-27 00:00:53
水仙花数 所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。例如,153是一水仙花数,因为153是1的3次方、5的3次方和3的3次方之和。 【问题描述】 输出所有的“水仙花数”,以空格隔开。 【示例代码】 # include <iostream> using namespace std ; void flower ( int m ) { int n = 0 , result = 0 , m1 = m ; while ( m > 0 ) { n = m % 10 ; m = m / 10 ; result = result + n * n * n ; } if ( result == m1 ) { cout << m1 << " " ; } } int main ( ) { for ( int i = 100 ; i < 1000 ; i ++ ) { flower ( i ) ; } cout << endl ; return 0 ; } 【举一反三】 可尝试输出其他种类的自幂数。 一位自幂数:独身数 两位自幂数:没有 三位自幂数:水仙花数 四位自幂数:四叶玫瑰数 五位自幂数:五角星数 六位自幂数:六合数 七位自幂数:北斗七星数 八位自幂数:八仙数 九位自幂数:九九重阳数 十位自幂数:十全十美数 来源: CSDN 作者: 雷雨肥️田 链接: https://blog

Console-算法[for,if]-一水仙花数(Water flower)

折月煮酒 提交于 2020-01-26 14:58:50
ylbtech-Arithmetic:Console-算法[for,if]-一水仙花数(Water flower) 水仙花数(Water flower) 1.A,Demo(案例) 打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数    本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。 1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。 2.程序源代码: 1.B,Solution(解决方案) using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { double b, s, g; //百位,十位,各位 Console.Write("'Water flower' number is:"); for (int i = 100; i < 1000; i++) { b = i / 100; //分解出百位 s = i / 10 % 10;//分解出十位 g = i % 10; //分解出个位 if (Math.Pow(b, 3) + Math.Pow(s, 3) + Math.Pow(g, 3) == i) { Console.Write("{0}\t",i

编程求\"水仙花数\"

血红的双手。 提交于 2020-01-26 00:35:52
所谓"水仙花数"是指一个三位数,其各位数字之和的立方等于该数本身,例如:153是水仙花数,因为153=1 3 +5 3 +3 3 代码实现: 一一列举for循环嵌套 1 #include<iostream> 2 using namespace std; 3 void main() 4 { 5 int a,b,c,m,n; 6 for(a=1;a<=9;a++) 7 for(b=1;b<=9;b++) 8 for(c=1;c<=9;c++) 9 {10 m=a*a*a+b*b*b+c*c*c;11 n=100*a+10*b+c;12 if(m==n)13 cout<<a<<' '<<b<<' '<<c<<endl;14 }15 }16 17 另一种解法: 1 #include<iostream> 2 using namespace std; 3 void main() 4 { 5 int a,b,c,m; 6 for(m=100;m<=999;m++) 7 { 8 a=m/100; 9 b=(m%100)/10; 10 c=m%10; 11 if(a*a*a+b*b*b+c*c*c==m) 12 cout<<a<<' '<<b<<' '<<c<<endl; 13 } 14 } 运行结果: 来源: https://www.cnblogs.com/iamvirus/archive

Python常见面试(习题)——水仙花数

ε祈祈猫儿з 提交于 2020-01-25 19:45:57
今天,给大家分享一个习题。 用python输出100到1000以内的水仙花数。 相信很多小伙伴都听到过,或者遇到过这个题目。 那么今天就来带大家做一做这道题。 首先,我们要知道什么是水仙花数, (@_@;)水仙花数是什么,我真的不知道啊??? 让我们来百度一下(内容来自百度百科)。 水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 n 位数(n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153) 哦哦~ 原来字幂数有这么多的叫法啊。 大家知道水仙花数是什么了吗? 黎梦来给大家总结一下: 水仙花数的范围:大于100,小于但不等于1000 它的每个位上的数字的 n 次幂之和等于它本身 既然我们知道了我们要筛选输出出来的水仙花数是什么样的,是不是就可以开始写程序了呢? 答案当然是肯定的! 那么问题来了,这个程序应该要怎么写? 首先,根据水仙花数的定义,我们是不是要把它三位数的每一位都取出来,然后取它的3次方,然后把这个三次方相加,检查总和是否与它本身相等就可以了。 其次,我们就该考虑怎么把每一位取出来了,这里有几个方法。 第一种方法

算法之水仙花数(Java语言)

☆樱花仙子☆ 提交于 2020-01-25 19:45:25
概述 在 数论 中, 水仙花 数 ( Narcissistic number ),也被称为 超完全数字不变数 ( pluperfect digital invariant, PPDI )、 自恋 数 、 自幂数 、 阿姆斯壮 数 或 阿姆斯特朗数 ( Armstrong number ) ,用来描述一个 N 位非负整数,其各位数字的 N 次方和等于该数本身。 举例 例如153、370、371及407就是三位超完全数字不变数,其各个数之立方和等于该数: 153 = 1 3 + 5 3 + 3 3 。 370 = 3 3 + 7 3 + 0 3 。 371 = 3 3 + 7 3 + 1 3 。 407 = 4 3 + 0 3 + 7 3 。 Java算法 1 /** 2 * A Narcissistic number is a number that is the sum of its own digits each 3 * raised to the power of the number of digits. E.g., 0, 1, 2, 3, 4, 5, 6, 7, 8, 4 * 9, 153, 370, 371, 407, 1634, 8208, 9474. 5 */ 6 public class NarcissisticNumberExample { 7 /

水仙花数

坚强是说给别人听的谎言 提交于 2020-01-25 19:43:34
/**打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各自数字 * 立方和等于该数本身。例如:153是一个”水仙花数“,因为153=1的三次方+5 * 的三次方+3的三次方。 */ public static void main(String[] args){ int j = 0; int i, k; int bai,Shi,ge; for (i = 100;i < 1000;i++){ bai = i/100; Shi = (i/10)%10; ge = (i%100)%10; k = bai * bai * bai + Shi * Shi * Shi + ge *ge *ge; if (i == k){ System.out.println(i); j++; } } System.out.println("符合条件的水仙花数为: "+j); }} 来源: https://www.cnblogs.com/qimengru/p/4605782.html

java 寻找水仙花数

风格不统一 提交于 2020-01-25 19:43:00
题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。 程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。 package Studytest; public class Prog3 { public static void main(String [] args) { for(int i=100;i<1000;i++) { if(isLotus(i)) { System.out.print(i+" "); } } } //判断水鲜花数 private static boolean isLotus(int lotus) { int bai=0; int shi=0; int ge=0; int sum=0; int n=0; n=lotus; bai=n/100; sum=bai*bai*bai; n=n-(bai*100); shi=n/10; sum+=(shi*shi*shi); ge=n-(shi*10); sum+=(ge*ge*ge); if(lotus==sum) { return true; }else { return false; } } } 运行结果: 来源: https://www.cnblogs.com

输出所有的水仙花数

青春壹個敷衍的年華 提交于 2020-01-25 19:42:40
需求:输出所有的”水仙花数” 所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。 举例:153就是一个水仙花数。 153 = 1 1 1 + 5 5 5 + 3 3 3 = 1 + 125 + 27 = 153 class Hello2 { public static void main(String[] args) { for (int i = 100;i <= 999 ;i++) { int ge = i % 10; int shi = i / 10 % 10; int bai = i / 10 / 10 % 10; if (i == ge * ge * ge + shi * shi *shi + bai * bai * bai) { System.out.println(i); } } } } 结果: 来源: https://www.cnblogs.com/Wangzui1127/p/11160429.html

水仙花数

让人想犯罪 __ 提交于 2020-01-25 19:42:02
public static void main(String[] args) { // 输出一千以内的水仙花数 /* * 水仙花数:一个三位数abc。 符合a*a*a+b*b*b+c*c*c=abc */ for (int i = 100; i < 1000; i++) { int a=i/100; int b=i/10%10; int c=i%10; if(a*a*a+b*b*b+c*c*c==i){ System.out.println(i); } } } 方法二: public static void main(String[] args) { // 输出一千以内的水仙花数 /* * 水仙花数:一个三位数abc。 符合a*a*a+b*b*b+c*c*c=abc */ for(int i=1; i<=9; i++) for(int j=0; j<=9; j++) for(int k=0; k<=9; k++) if(i*i*i+j*j*j+k*k*k == 100*i+10*j+k) System.out.print(i*100+j*10+k+" "); } 来源: https://www.cnblogs.com/gxr-boke/p/9960693.html