编写一个Java应用程序,该应用程序包括2个类:Print类和主类E。Print 类里有一个方法output()功能是输出100 ~ 999之间的所有水仙花数(各位数字的 立方和等于这个三位数本身,如: 371 = 33 + 73 + 13。)在主类E的main方法中来 测试类Print。

package com.homework.zw;
//print类部分
public class Print
{
    void output()
    {
        for(int i =100;i<=999;i++)
        {
            if(Math.pow(i/100,3)+Math.pow(i%10,3)+Math.pow(i/10%10, 3)==i)
            {
                System.out.println(i);
            }
        }
    }
}

package com.homework.zw;
//主类E部分
public class E
{

    public static void main(String[] args)
    {
        Print pr = new Print();
        pr.output();
    }

}

时间: 2024-08-23 21:36:43

编写一个Java应用程序,该应用程序包括2个类:Print类和主类E。Print 类里有一个方法output()功能是输出100 ~ 999之间的所有水仙花数(各位数字的 立方和等于这个三位数本身,如: 371 = 33 + 73 + 13。)在主类E的main方法中来 测试类Print。的相关文章

编写一个程序找出100~999之间所有的水仙花数

如果一个3位数等于其各位的立方和,称该数为水仙花数. 如,所以407是一个水仙花数,编写一个程序找出100~999之间所有的水仙花数. 1 #include<stdio.h> 2 #include<stdlib.h> 3 //判断水仙花数,是则返回1 4 int isNarcissus(int n); 5 6 int main() 7 { 8 int i; 9 for(i = 100; i < 1000; i++) 10 if(isNarcissus(i)) 11 print

【python】编写一个程序,求100~999之间的所有水仙花数

编写一个程序,求100~999之间的所有水仙花数. 如果一个三位数等于其各位数字的立方和,则称这个数为水仙花数. 例如:153=1^3+5^3+3^3 因此153就是一个水仙花数 代码如下 #水仙花数 for i in range(100, 1000): sum = 0 temp = i while temp: sum = sum + (temp%10)**3 temp //=10 if sum == i: print(i) 原文地址:https://www.cnblogs.com/SiminL

c语言:3种方法;求出0~999之间的所有“水仙花数”并输出。

方法一: #include <stdio.h> int main() { int i,j,k,n; printf("水仙花数:",n); for(n=100;n<1000;n++) { i=n/100; j=n/10-i*10; k=n%10; if(n==i*i*i+j*j*j+k*k*k) printf("%d\n ",n); } return 0; } 输出结果: 水仙花数:153 370 371 407 Press any key to c

C语言:求出0~999之间的所有“水仙花数”并输出

#include <stdio.h> int main() {  int num,unit,ten,hundred;  int i=0;  printf("水仙花数:\n");  for(num=100;num<=999;num++)  {   hundred=num/100;   ten=(num-hundred*100)/10;   unit=num-hundred*100-ten*10;    if(num==hundred*hundred*hundred+te

C语言 求出100~999之间的所有“水仙花数”并输出

"水仙花数"是指一个三位数,其各位数字的立方和确好等于该数本身,如:153=1+5+3?,则153是一个"水仙花数".在数论中,水仙花数(Narcissistic number)也称为自恋数.自幂数.阿姆斯壮数或阿姆斯特朗数(Armstrong number),是指一N位数,其各个数之N次方和等于该数. 例如153.370.371及407就是三位数的水仙花数,其各个数之立方和等于该数: 153 = 1^3 + 5^3 + 3^3. 370 = 3^3 + 7^3 +

求出0~999之间的所有“水仙花数”并输出

#include <stdio.h> int main() { int num,a,b,c;  for(num=100;num<=999;num++)    { a=num/100; b=(num-a*100)/10; c=(num-a*100-b*10); if(num==a*a*a+b*b*b+c*c*c) { printf("%d ",num); } }     return 0; }

利用c语言求出0~999之间的所有“水仙花数”并输出

#include <stdio.h> #include <math.h> int main() { int m; int bai=0; int shi=0; int ge=0; int you=0; for(m=100;m<=999;m++) { bai=m/100; shi=(m%100)/10; ge=m%10; you=pow(bai,3)+pow(shi,3)+pow(ge,3); if(m==you) printf("%d ",m); } ret

Python基础练习-003-求100-999之间所有的水仙花数

水仙花数是指一个n位的正整数(n>=3),它的每个数字的n次幂之和等于它本身.例如:153=1*1*1+5*5*5+3*3*3,153为三位数,它的每个数字的三次方之和等于153.用python语言实现求出100~999之间的所有水仙花数. 分析过程:将正整数n的个位十位百位分别取出,再求幂之和:个位百位比较好拆分,十位的话可以先减去百位再用除法计算. 1 # -*- coding:utf-8 -*- 2 # @Author : 飘飘_emmm 3 print("100-999之间的水仙花

JS 1000以内的水仙花数 (三位数 各个数字的立方和等于本身 例如 1*1*1 + 5*5*5 + 7*7*7 = 157)

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> </body> </html> <script type="text/javascript"> for(i=100;i<1000;i++){ var a = par