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;
}
}
/**
*运算规则:
*水仙花数是三位数,它的个、十、百位数字的立方的和等于该数本身,
*例如: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;
}
}