java入门练习题三

时间:2023-02-24 00:12:44

1.水仙花数 :每个位的数的立方和 等于这个数 

package com.pratice.daily;

public class ShuiXianHuaNum {
public void findShuXianHuaNum(){
for(int i=100;i<1000;i++){
int a = i%100%10;
int b = i/10%10;
int c = i/100;
int d = (int) (Math.pow(a, 3)+Math.pow(b, 3)+Math.pow(c, 3));
if (d==i)
System.out.println(i);
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
ShuiXianHuaNum sxhn = new ShuiXianHuaNum();
sxhn.findShuXianHuaNum();
}
}

2.分解质因数

package com.pratice.daily;

public class DecomposeNUM {

public void decompose(int Num){
if(Num <=2){
System.out.println("Number must be larger than 2");
return;
}
int[] nums = null;
nums = new int[8];
int j=0;
int i=2;
while(true){
for(i=2;i<Num;i++){
if(i>=Num) break;
else if(Num%i==0){
nums[j] =i;
j++;
Num = Num/i;
break;

}else
continue;
}
if(i>=Num){
if(j==0) return;
else {
nums[j]=Num;
for(int a=0;a<j;a++)
System.out.print(nums[a]+"*");
System.out.print(nums[j]+"");
return;
}
}
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
DecomposeNUM dnum = new DecomposeNUM();
dnum.decompose(100);

}

}