2020年3月5日 星期四

[zerojudge]a044. 空間切割

a044. 空間切割

圖1 圖2
參考影片:(DM10-20131118-03) 平面分割空間

沒有用lineCut和spaceCut是因為會超時TLE(Time Limit Exceed)

程式碼如下:

  1. /* a044. 空間切割
  2. *
  3. * 2020/3/5
  4. */
  5.  
  6. import java.util.Scanner;
  7.  
  8. public class Pa044{
  9.  
  10. public static void main(String[] args){
  11. Scanner scanner = new Scanner(System.in);
  12.  
  13. while(scanner.hasNext()){
  14. System.out.println(spaceCutSuper(scanner.nextInt()));
  15. }
  16. }
  17.  
  18. public static int lineCut(int n){
  19. return n == 0? 1: lineCut(n - 1) + n;
  20. }
  21.  
  22. public static int spaceCut(int n){
  23. return n == 0? 1: spaceCut(n - 1) + lineCut(n - 1);
  24. }
  25.  
  26. public static int spaceCutSuper(int n){
  27. return (n * n * n + 5 * n + 6 ) / 6;
  28. }
  29. }

沒有留言:

張貼留言