andy6804tw
8/18/2016 - 3:36 AM

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1264 這題就是判斷階層結果大於6227020800就Overflow小於1000(

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1264

這題就是判斷階層結果大於6227020800就Overflow小於1000(不包含負數)Underflow 當負數時偶數-2 -4 -6...為Underflow 奇數-1 -3 -5...為Overflow 先利用DP建立1001個階層表當輸入值大於1000直接Overflow 補充: 比較大小 int i=b1.compareTo(b2)
i可能為-1、0、1,分别表示小於、等於、大於 i=-1 ==> b1<b2 i=0 ==> b1=b2 i=1 ==> b1>b2

import java.math.BigInteger;
import java.util.*;  
  
public class Main {  
  
    public static void main(String[] args) {  
        Scanner scn = new Scanner(System.in);  
        BigInteger arr[]=new BigInteger[1001];
        arr[1]=new BigInteger("1");arr[0]=new BigInteger("1");
        for(int i=2;i<=1000;i++){
        	arr[i]=arr[i-1].multiply(new BigInteger(Integer.toString(i)));
        }
       while(scn.hasNext()){
    	  int num=scn.nextInt(),sum=0;
    	  if(num<0){
    		  if(num%2==0)
    			  System.out.println("Underflow!");
    		  else
    			  System.out.println("Overflow!");
    	  }
    	  else if(num>1000||arr[num].compareTo(new BigInteger("6227020800"))==1)
    		  System.out.println("Overflow!");
    	  else if(arr[num].compareTo(new BigInteger("10000"))==-1)
    		  System.out.println("Underflow!");
    	  else
    		  System.out.println(arr[num]);
       }
    } 
    /* 
    題目:Q10323 - Factorial! You Must be Kidding!!!
    作者:1010
    時間:西元 2016 年 8 月 */
}