http://www.spoj.pl/problems/FCTRL2/
要點(diǎn):
1、BigInteger屬于java.math.BigInteger,因此在每次使用前都要import 這個(gè)類。
2、其常用的構(gòu)造方法有:
BigInteger(String val)
將 BigInteger 的十進(jìn)制字符串表示形式轉(zhuǎn)換為 BigInteger。 |
BigInteger(String val, int radix)
將指定基數(shù)的 BigInteger 的字符串表示形式轉(zhuǎn)換為 BigInteger。 |
如要將int型的2轉(zhuǎn)換為BigInteger型,要寫(xiě)為BigInteger two=new BigInteger("2"); //
注意2雙引號(hào)不能省略
3、BigInteger類模擬了所有的int型數(shù)學(xué)操作,如add()==“+”,divide()==“-”等,但注意其內(nèi)容進(jìn)行數(shù)學(xué)運(yùn)算時(shí)不能直接使用數(shù)學(xué)運(yùn)算符進(jìn)行運(yùn)算,必須使用其內(nèi)部方法。而且其操作數(shù)也必須為BigInteger型。
如:two.add(2)就是一種錯(cuò)誤的操作,因?yàn)?沒(méi)有變?yōu)锽igInteger型。
4、當(dāng)要把計(jì)算結(jié)果輸出時(shí)應(yīng)該使用.toString方法將其轉(zhuǎn)換為10進(jìn)制的字符串,詳細(xì)說(shuō)明如下:
輸出方法:System.out.print(two.toString());
5、三個(gè)經(jīng)常用到的函數(shù):
remainder用來(lái)求余數(shù)。
negate將操作數(shù)變?yōu)橄喾磾?shù)。
compare的詳解如下:
compareTo
public int compareTo(BigInteger val)
將此 BigInteger 與指定的 BigInteger 進(jìn)行比較。對(duì)于針對(duì)六個(gè)布爾比較運(yùn)算符 (<, ==, >, >=, !=, <=) 中的每一個(gè)運(yùn)算符的各個(gè)方法,優(yōu)先提供此方法。執(zhí)行這些比較的建議語(yǔ)句是:(x.compareTo(y) <op> 0),其中 <op> 是六個(gè)比較運(yùn)算符之一。
指定者:接口 Comparable<BigInteger>
中的 compareTo
參數(shù): val
- 將此 BigInteger 與之比較的 BigInteger。
返回: 當(dāng)此 BigInteger 在數(shù)值上小于、等于或大于 val 時(shí),返回 -1,0,或 1
import java.util.*;
import java.io.*;
import java.math.*;


public class SPOJ_24
{
public static void main(String rgs[]) throws Exception

{
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
String line = stdin.readLine();
int i,j,k,n = Integer.parseInt(line);

for(i=0;i<n;i++)
{
line = stdin.readLine();
k = Integer.parseInt(line);
BigInteger a = BigInteger.valueOf(1);
BigInteger c = BigInteger.valueOf(1);

for(j=2;j<=k;j++)
{
BigInteger b = BigInteger.valueOf(j);
c = a.multiply(b);
a = c;
}
System.out.println(c);
}
}
}
posted on 2009-08-21 10:39
飛翔天使 閱讀(180)
評(píng)論(0) 編輯 收藏 所屬分類:
spoj