资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java代码实现水仙花数 java编写水仙花数

java求水仙花数

按照你的要求编写的求N位水仙花数的程序如下

旌德网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联公司于2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司

import java.util.Scanner;

public class CCC {

public static void main(String[] args) {

System.out.print("请输入正整数N(3=N=7):");

Scanner sc=new Scanner(System.in);

int N=sc.nextInt();

if(N3 || N7){

System.out.println("N3 或者 N7");

return;

}

int min=(int)Math.pow(10, N-1);

int max=(int)Math.pow(10, N);

for(int i=min;imax;i++){

int tmp=i;

int j=0;

int a[]=new int[N];

while(tmp!=0){

a[j]=tmp%10;

tmp=tmp/10;

j++;

}

int sum=0;

for(int k=0;kN;k++){

sum=sum+(int)Math.pow(a[k],N);

}

if(sum==i){

System.out.println(i);

}

}

}

}

运行结果

请输入正整数N(3=N=7):4

1634

8208

9474

求水仙花数的java程序代码

public class Daffodil {

/**

*

* @param

* @return void

* @param args

* desc

*/

public static void main(String[] args) {

for (int n = 100; n 999; n++) {

int a = n / 100;

int b = (n % 100) / 10;

int c = n % 10;

if(Math.pow(a, 3)+Math.pow(b,3)+Math.pow(c,3)==n){

System.out.println(n);

}

}

}

}

如何用java实现任意位数的水仙花数?

你好,代码如下:

public class ShuiXianHua {

public static void calculate(int num) {

int[] nums;

int temp = 0, cur;

int min = (int) Math.pow(10, num - 1);

int max = (int) Math.pow(10, num) - 1;

for (int i = min; i = max; i++) {

temp = 0;

nums = new int[num];

cur = i;

for (int j = 0; j num; j++) {

nums[j] = cur % 10;

cur /= 10;

}

for (int j = 0; j nums.length; j++) {

temp += (int) Math.pow(nums[j], num);

}

if (temp == i) {

System.err.println(i);

}

}

}

public static void main(String[] args) {

System.out.println("6位的水仙花数:" );

calculate(6) ;

}

}

用Java写个关于“水仙花数”的程序?

按一下代码执行:

public class woo {

public static void main(String args[]) {

System.out.println("100-1000中的水仙花数有:");

for(int i=100;i1000;i++){

int single  = i%10;

int ten = i/10%10;

int hundred = i/10/10%10;

//水仙花数判断要求

if(i == (single*single*single+ten*ten*ten+hundred*hundred*hundred)){

System.out.println(i);

}

}

}

}

扩展资料:

水仙花数只是自幂数的一种,严格来说3位数的3次幂数才称为水仙花数。

一位自幂数:独身数

两位自幂数:没有

三位自幂数:水仙花数

四位自幂数:四叶玫瑰数

五位自幂数:五角星数

六位自幂数:六合数

七位自幂数:北斗七星数

八位自幂数:八仙数

九位自幂数:九九重阳数

十位自幂数:十全十美数

参考资料:

水仙花数——百度百科


当前题目:java代码实现水仙花数 java编写水仙花数
转载来源:http://cdkjz.cn/article/ddcehgo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220