标题: 高手请进 [打印本页] 作者: 权跃杰 时间: 2012-7-31 16:20 标题: 高手请进 老师或高手请进!!
对比下列2段代码:
代码1
class A{
public static void main(String args[]){
System.out.println("1!+2!+3!+4!+...+18!+19!+20!="+sum()) ;
}
public static long sum(){ //求出所有数字阶乘的和
long sum = 0l ;
for(int i = 1 ; i <= 20 ; i++){
sum+=num(i) ;
}
return sum ;
}
public static double num(int x) { //求出单个数字的阶乘是多少
if(x==0||x==1){
return 1 ;
}else{
return x * num(x-1) ;
}
}
}
代码2
public class Practice11{
public static void main(String args[]){
long sum = 0L;
for (int x = 1; x<=20; x++){
sum += Factorial(x);
}
System.out.println("1!+2!+3!+...+20!结果是:" + sum);
}
public static long Factorial(int k){
if (k == 0||k == 1){
return 1;
}else{
return k * Factorial(k-1);
}
}
}