
正文
java《水仙花数》代码 java中水仙花数的代码怎么写
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java编程求水仙花数
一个是0到999循环带入
一个是多层循环
区别应该在代码性能上
可以将数值加大 然后使用程序运行时间进行对比一下
第2个 abc都为0java《水仙花数》代码的时候 也能成立啊java《水仙花数》代码,所以java《水仙花数》代码你应该 修改成从1开始初始化abc
相关问答
Q1: java求水仙花数
按照java《水仙花数》代码你java《水仙花数》代码的要求编写的求N位水仙花数的程序如下
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
Q2: 用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次幂数才称为水仙花数。
一位自幂数:独身数
两位自幂数:没有
三位自幂数:水仙花数
四位自幂数:四叶玫瑰数
五位自幂数:五角星数
六位自幂数:六合数
七位自幂数:北斗七星数
八位自幂数:八仙数
九位自幂数:九九重阳数
十位自幂数:十全十美数
参考资料:
水仙花数——百度百科
Q3: 菜鸟课堂:教你如何在Java里实现水仙花数
今天偶然发现这个,分享给大伙。在Java里也实现,希望对初学者有所帮助。 以下是代码内容: package shuixianhuashu; /** *运算规则: *水仙花数是三位数,它的个、十、百位数字的立方的和等于该数本身, *例如:371=27+343+1 *求出所有水仙花数(分别用for,while循环实现) * @author S10 * */ public class ShuiXianHuaShu { public static void main(String[] args) { int count = 0; for(int i = 100; i 1000; i ++){ if(getCount(i)){ System.out.println(i + "是水仙花数....."); count++; } } //上下两个循环的效果是一样的 int i = 100; while(i 999){ i++; if(getCount(i)){ System.out.println(i + "是水仙花数===="); count++; } } System.out.println("共有" + count + "个水仙花数!"); } //得到一个数的立方的值 public static int getLifang(int i){ return i * i * i ; } public static boolean getCount(int i){ int a = i / 100; //取出百位的的数字 int temp = i % 100; int b = temp / 10; //取出十位的数字 int c = temp % 10; //取出各位的数字 if(getLifang(a) + getLifang(b) + getLifang(c) == i){ return true; }else{ return false; } } } (责任编辑:董建伟)
Q4: 如何用java实现任意位数的水仙花数?
java《水仙花数》代码你好java《水仙花数》代码,代码如下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位java《水仙花数》代码的水仙花数:" );
calculate(6) ;
}
}
Q5: 求水仙花数的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《水仙花数》代码和java中水仙花数的代码怎么写的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








