居左


私信TA

用户名:JZ50

访问量:70350

签 名:

楼下你的分数已经再次被超越!!快刷!!

等  级
排  名 32
经  验 13553
参赛次数 2
文章发表 109
年  龄 0
在职情况 学生
学  校 99
专  业

  自我简介:

解题思路:

   经典递归求阶乘解法!



注意事项:

   位数较多时,会出现精度不够的情况,使用 BigInteger 类进行数据处理。



参考代码:

import java.math.BigInteger;
import java.util.Scanner;

public class C1014 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			BigInteger rs = F(sc.nextInt());
			System.out.println(rs);
		}
		sc.close();
	}
	
	private static BigInteger F(int n){
		if(n == 1)
			return BigInteger.ONE;
		return F(n-1).add(S(n));
	}
	
	private static BigInteger S(int n){
		if(n == 1)
			return BigInteger.ONE;
		return S(n-1).multiply(new BigInteger(String.valueOf(n)));
	}
}


 

0.0分

0 人评分

  评论区