n >= 6 的时候减3, 最后分类讨论, 上个Java
1 import java.math.BigInteger; 2 import java.util.*; 3 4 5 public class Main { 6 public static void main(String[] args) { 7 Scanner in = new Scanner(System.in); 8 int n = in.nextInt(); 9 BigInteger ans = BigInteger.valueOf(1); 10 while(n >= 6) { 11 ans = ans.multiply(BigInteger.valueOf(3)); 12 n -= 3; 13 } 14 15 if(n == 5) { 16 ans = ans.multiply(BigInteger.valueOf(6)); 17 } else if(n == 4) { 18 ans = ans.multiply(BigInteger.valueOf(4)); 19 } else if(n == 3) { 20 ans = ans.multiply(BigInteger.valueOf(3)); 21 } else if(n == 2) { 22 ans = ans.multiply(BigInteger.valueOf(2)); 23 } 24 25 String s = ans.toString(); 26 System.out.println(s.length()); 27 for(int i = 0; i < s.length() && i < 100; i++) { 28 System.out.print(s.charAt(i)); 29 } 30 } 31 }