Java实现蓝桥杯VIP 算法训练 阶乘末尾

试题 算法训练 阶乘末尾

资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
  给定n和len,输出n!末尾len位。
输入格式
  一行两个正整数n和len。
输出格式
  一行一个字符串,表示答案。长度不足用前置零补全。
样例输入
6 5
样例输出
00720
数据规模和约定
  n<=30, len<=10。

package 第十次模拟;

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

public class 阶乘末尾 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int len = sc.nextInt();
		sc.close();
		BigInteger  num1 = new BigInteger(n+"");
		for (int i = 2; i <n; i++) {
			BigInteger  num2 = new BigInteger(i+"");
			num1=num1.multiply(num2);
		}
		String s = num1+"";
		if(s.length()>len){
			s=s.substring(s.length()-len);
		}
		else{
			while(s.length()<len){
				s="0"+s;
			}
		}
		System.out.println(s);
  	}

}

发布了1411 篇原创文章 · 获赞 1万+ · 访问量 148万+

猜你喜欢

转载自blog.csdn.net/a1439775520/article/details/104669206