欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

阶乘计算

程序员文章站 2022-03-09 16:11:25
...

阶乘计算

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int[] value = new int[9999];
		value[0] = 1;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j < value.length-1; j++) {
				value[j] = value[j] * i;
			}
			for (int j = 0; j < value.length-1; j++) {
				value[j+1] = value[j+1] + (value[j]/10);
				value[j] = value[j] % 10;
			}
		}
		int index = 0;
		for (int i = value.length-1; i >= 0; i--) {
			if(value[i] != 0) {
				index = i;
				break;
			}
		}
		for (int i = index; i >= 0; i--) {
			System.out.print(value[i]);
		}
	}
}
相关标签: 蓝桥杯