
正文
java中水仙花代码 水仙花数java代码用for语句
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用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次幂数才称为水仙花数。
一位自幂数:独身数
两位自幂数:没有
三位自幂数:水仙花数
四位自幂数:四叶玫瑰数
五位自幂数:五角星数
六位自幂数:六合数
七位自幂数:北斗七星数
八位自幂数:八仙数
九位自幂数:九九重阳数
十位自幂数:十全十美数
参考资料:
水仙花数——百度百科
相关问答
Q1: 求水仙花数的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);
}
}
}
}
Q2: 怎样用Java编写“水仙花”
水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3+ 3^3 = 153)
Q3: java的水仙花束程序
法一java中水仙花代码:import java.awt.*;
import java.applet.*;
import java.math.*;
public class Shuixianhua extends Applet {
public void init() {
}
public void paint(Graphics g) {
//定义相关变量
int elem[]=new int[4];
int num,temp;
double total;
int row=20,column=30;
int count=0,k;
g.drawString("4位java中水仙花代码的水仙花数如下所示:", 20, 30 );
//利用循环寻找1000到10000之间的水仙花数
for(num=1000; num10000; num++)
{
k=0;
temp=num;
//提取num中的千位java中水仙花代码,百位java中水仙花代码,十位java中水仙花代码,个位,存储在整型数组elem[4]中
do
{ elem[k]=temp%10;
temp=temp/10;
k++;
}while(!(temp==0));
total=Math.pow(elem[0],4)+Math.pow(elem[1],4)+Math.pow(elem[2],4)+Math.pow(elem[3],4);
//判断是否未水仙花数
if(total==num)
{
count++;
//输出格式控制
if(count%8==0)
{
row=row+25;
}
else
{
column=column+30;
}
g.drawString(num+"是水仙花数",row,column);
}
column=column+30;
g.drawString("共"+count+"个",row,column);
}
}
法二:public class Sxhs{
public static void main(String[] agrs){
int a1 , a2 , a3;
for(int i=1000 ; i10000 ; i++){
a1 = i / 1000;
a2=(i-a1*1000)/100;
a3=i-a1*1000-a2*100;
if(i==(a1*a1*a1)+(a2*a2*a2)+(a3*a3*a3)){
System.out.println("shi : " + i);
}
}
}
}
法三:public class shuixianhua{
public static void main(String[] agrs){
int a1 , a2 , a3,a4;
for(int i=1000 ; i10000 ; i++){
a1 = i / 1000;
a2=(i-a1*1000)/100;
a3=(i-a1*1000-a2*100)/10;
a4=i-a1*1000-a2*100-a3*10;
if(i==(a1*a1*a1*a1)+(a2*a2*a2*a2)+(a3*a3*a3*a3)+(a4*a4*a4*a4)){
System.out.println("shi : " + i);
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);
}
}
}
}
法四:public class Suixian {
public static void main(String[] args) throws java.io.IOException {
byte[] buf = new byte[20];
int cmdLength = System.in.read(buf);
String str = new String(buf,0,cmdLength-2);
int n = Integer.parseInt(str);//这里当然是输入一个位数罗,也不一定就三位吧
int low = 1,high = 1;
for (int i=1;in;i++) low = low*10;
high = low * 10;
//System.out.println(low);
//System.out.println(high);
for (int i=low;ihigh;i++)
{
int sum = 0;
int p = i;
while (p!=0)
{
int r = p%10;
p = p/10;
int rn = 1;
for (int j=1;j=n;j++) rn = rn * r;
sum = sum + rn;
}
if (sum==i) System.out.println;
}
}
}
不过这只是法一的一个化简,(法一用了一个数组)
第二个方法是这样,拿三未数举例
public class SuiXian2 {
public static void main(String[] args) {
for (int a=1;a=9;a++)
for (int b=0;b=9;b++)
for (int c=0;c=9;c++)
if(a*a*a+b*b*b+c*c*c==100*a+10*b+c)
System.out.println(100*a+10*b+c);
}
}
Q4: java编程 1到1000的水仙花数 求教
所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。代码如下
public class Shuixianhua {
/**
* 判断数字是否是水仙花数
* @Title: shuixianhua
* @param:@param x
* @param:@return
* @return:boolean
* @Description:
* @date 2017年11月2日 下午3:03:10
* @throws
*/
public boolean shuixianhua(int x)
{
int i=0,j=0,k=0;
i=x / 100;
j=(x % 100) /10;
k=x % 10;
if(x==i*i*i+j*j*j+k*k*k)
return true;
else
return false;
}
public static void main(String[] args) {
//存放所有水仙花数的集合
ListInteger sxhList=new ArrayListInteger();
Shuixianhua sxh=new Shuixianhua();
for(int i=1;i=1000;i++){
//判断是否是水仙花数,是则放入集合
if(sxh.shuixianhua(i)){
sxhList.add(i);
}
}
//打印所有水仙花数
System.out.println(sxhList.toString());
}
}
关于java中水仙花代码和水仙花数java代码用for语句的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







