java 实现阶乘 三种方法
用三种方法来实现n!实现使用BigDecimal类,因为用int最多正确算到12!,用long最多正确算到20!.废话不多说,直接上代码:
//第一种方法:
Java代码
- 1.package Factorial;
- 2.
- 3.import java.math.BigDecimal;
- 4.import java.util.Scanner;
- 5.public class Factorial1 {
- 6. public static BigDecimal factorial(int n){
- 7. BigDecimal result = new BigDecimal(1);
- 8. BigDecimal a;
- 9. for(int i = 2; i <= n; i++){
- 10. a = new BigDecimal(i);
- 11. result = result.multiply(a);
- 12. }
- 13. return result;
- 14. }
- 15.
- 16. public static void main(String[] arguments){
- 17. Scanner sc = new Scanner(System.in);
- 18. int a = sc.nextInt();
- 19. System.out.println(a + "!=" + factorial(a));
- 20. }
- 21.
- 22.}
- package Factorial;
- import java.math.BigDecimal;
- import java.util.Scanner;
- public class Factorial1 {
- public static BigDecimal factorial(int n){
- BigDecimal result = new BigDecimal(1);
- BigDecimal a;
- for(int i = 2; i <= n; i++){
- a = new BigDecimal(i);
- result = result.multiply(a);
- }
- return result;
- }
-
- public static void main(String[] arguments){
- Scanner sc = new Scanner(System.in);
- int a = sc.nextInt();
- System.out.println(a + "!=" + factorial(a));
- }
- }
第二种方法:
Java代码
- 1.//第二种方法
- 2.package Factorial;
- 3.
- 4.import java.math.BigDecimal;
- 5.import java.util.Scanner;
- 6.public class Factorial3 {
- 7. public static BigDecimal factorial(BigDecimal n){
- 8. BigDecimal bd1 = new BigDecimal(1);
- 9. BigDecimal bd2 = new BigDecimal(2);
- 10. BigDecimal result = bd1;
- 11. while(n.compareTo(bd1) > 0){
- 12. result = result.multiply(n.multiply(n.subtract(bd1)));
- 13. n = n.subtract(bd2);
- 14. }
- 15. return result;
- 16. }
- 17. public static void main(String[] arguments){
- 18. Scanner sc = new Scanner(System.in);
- 19. BigDecimal n = sc.nextBigDecimal();
- 20.
- 21. System.out.print(n + "!=" + factorial(n));
- 22. }
- 23.
- 24.}
//第二种方法
- package Factorial;
- import java.math.BigDecimal;
- import java.util.Scanner;
- public class Factorial3 {
- public static BigDecimal factorial(BigDecimal n){
- BigDecimal bd1 = new BigDecimal(1);
- BigDecimal bd2 = new BigDecimal(2);
- BigDecimal result = bd1;
- while(n.compareTo(bd1) > 0){
- result = result.multiply(n.multiply(n.subtract(bd1)));
- n = n.subtract(bd2);
- }
- return result;
- }
- public static void main(String[] arguments){
- Scanner sc = new Scanner(System.in);
- BigDecimal n = sc.nextBigDecimal();
-
- System.out.print(n + "!=" + factorial(n));
- }
- }
第三种方法,用递归实现:
Java代码
- 1.package Factorial;
- 2.
- 3.import java.util.Scanner;
- 4.import java.math.BigDecimal;
- 5.public class Factorial2 {
- 6. public static BigDecimal factorial(BigDecimal n){
- 7. BigDecimal bd1 = new BigDecimal(1);
- 8. if(n.equals(new BigDecimal(1))){
- 9. return bd1;
- 10. }
- 11. else
- 12. return n.multiply(factorial(n.subtract(bd1)));
- 13. }
- 14. public static void main(String[] arguments){
- 15. Scanner sc = new Scanner(System.in);
- 16. BigDecimal a = sc.nextBigDecimal();
- 17. BigDecimal result = factorial(a);
- 18. System.out.println(a + "!=" +result);
- 19.
- 20. }
- 21.
- 22.}