代码public class why {
public static void main(String args[]){
System.out.println(factorial(28));
System.out.println(factorial(35));
System.out.println(factorial(26));
System.out.println(factorial(4));
}
public static long factorial(long number){
if(number <= 1)
return 1;
else
return number*factorial(number -1 );
}
}
运行结果
-5968160532966932480
6399018521010896896
-1569523520172457984
24
希望高手帮忙解决一下,小弟感激不尽
public static void main(String args[]){
System.out.println(factorial(28));
System.out.println(factorial(35));
System.out.println(factorial(26));
System.out.println(factorial(4));
}
public static long factorial(long number){
if(number <= 1)
return 1;
else
return number*factorial(number -1 );
}
}
运行结果
-5968160532966932480
6399018521010896896
-1569523520172457984
24
希望高手帮忙解决一下,小弟感激不尽
public static void main(String args[]){
for(int i=0; i<30; i++) {
System.out.println(i + "!= " + factorial(i));
}
}
public static long factorial(long number){
if(number <= 1)
return 1;
else
return number*factorial(number -1 );
}
}输出:
0!= 1
1!= 1
2!= 2
3!= 6
4!= 24
5!= 120
6!= 720
7!= 5040
8!= 40320
9!= 362880
10!= 3628800
11!= 39916800
12!= 479001600
13!= 6227020800
14!= 87178291200
15!= 1307674368000
16!= 20922789888000
17!= 355687428096000
18!= 6402373705728000
19!= 121645100408832000
20!= 2432902008176640000
21!= -4249290049419214848
22!= -1250660718674968576
23!= 8128291617894825984
24!= -7835185981329244160
25!= 7034535277573963776
26!= -1569523520172457984
27!= -5483646897237262336
28!= -5968160532966932480
29!= -7055958792655077376
但为什么 23的不溢出呢?而21的溢出?都溢出了,包括22,只不过java溢出的话取后几位,不会抱错
定义一个int[] a;
a[0] = 123;
a[1] = 456;
a[2] = 789;
来表示数据123*1000000+456*1000+789
呵呵,以前见过这样做的
换为double 就不会有益处了