黒馬プログラマ——階乗の2つの実現方法と水仙数の印刷
--------Javaトレーニング、Androidトレーニング、iOSトレーニング、Netトレーニング、期待とあなたの交流!-----。
需要:循環と再帰印刷5をそれぞれ使用する階乗
需要:循環と再帰印刷5をそれぞれ使用する階乗
/*
* 5
* 1.
* 2.
*/
public class Factorial5 {
public static void main(String[] args) {
System.out.println(factorialM1(5));
System.out.println(factorialM2(5));
}
// n
public static int factorialM1(int n) {
int y = 1;
for (int x = 1; x <= n; x++) {
y *= x;
}
return y;
}
// n
/*
* :
* n==1,return n;
* n!=1,return n*jc(n-1)
*/
public static int factorialM2(int n) {
if(n==1){
return n;
}else{
return n*factorialM2(n-1);
}
}
}
プリントの水仙の数:三桁の数を指します。各数字の立方とはこの数自体です。/*
*
* , 。
* :
* 3 100-999, for
* , ,
* :n%10
* :n/10%10
* :n/100%10
*
*/
public class PrintDaffodil {
public static void main(String[] args) {
for(int x=100;x<1000;x++){
int ge = x%10;
int shi = x/10%10;
int bai = x/100%10;
if(ge*ge*ge+shi*shi*shi+bai*bai*bai==x){
System.out.println(x);
}
}
}
}