51nod1059:超大数阶乘

  • 每1000位换行,可以在for循环的打印中使用小技巧判断(i + 1) % 1000 == 0,成立则输出一次换行。
  • 存储用大数,输出用字符。
import java.util.Scanner;
import java.math.BigInteger;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		BigInteger n = in.nextBigInteger();
		BigInteger a = BigInteger.ONE, ans = a;
		while (a.compareTo(n) <= 0) {//ans存储n!
			ans = ans.multiply(a);
			a = a.add(BigInteger.ONE);
		}
		String s = ans.toString();//转化为字符后打印
		for (int i = 0; i < s.length(); i++) {
			System.out.print(s.charAt(i));
			if ((i + 1) % 1000 == 0)
				System.out.println("");
		}
	}
}