大数模板

大数阶乘

题目链接http://acm.nyist.net/JudgeOnline/problem.php?pid=28

代码如下:

[java]  view plain copy
  1. import java.io.*;  
  2. import java.math.BigInteger;  
  3. import java.util.*;  
  4.   
  5. public class Main  
  6. {  
  7.     static void main(String args[])  
  8.     {  
  9.         Scanner cin = new Scanner(system.in);     
  10.         int n = cin.nextInt();  
  11.         BigInteger ans = BigInteger.ONE;  
  12. for(int i = 1; i <= n; ++i)  
  13.             ans = ans.multiply(BigInteger.valueOf(i));  
  14.         System.out.println(ans);  
  15.     }  
  16. }  


棋盘覆盖

题目链接http://acm.nyist.net/JudgeOnline/problem.php?pid=45

代码如下:

copy
    import java.math.BigInteger;  
  1. import java.util.*;  
  2.         Scanner in = new Scanner(system.in);  
  3. int test = in.nextInt();  
  4.         while(test-- > 0)  
  5.         {  
  6.             int n;  
  7.             n = in.nextInt();  
  8.             BigInteger a = new BigInteger("4");  
  9.             1; i < n; ++i)  
  10.                 a = a.multiply(BigInteger.valueOf(4));  
  11.             System.out.println(a.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3)));  
  12.         }  
  13.     }  
  14. }  

比较大小

题目链接http://acm.nyist.net/JudgeOnline/problem.php?pid=73

代码如下:

[java]  view plain copy
    import java.io.*;  
  1. import java.math.BigInteger;  
  2. import java.util.*;  
  3.   
  4. class Main  
  5. {  
  6. void main(String args[])  
  7.     {  
  8. new Scanner(system.in);     
  9. while(cin.hasNext())  
  10.         {  
  11.             BigInteger a = cin.nextBigInteger();  
  12.             BigInteger b = cin.nextBigInteger();  
  13. if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO))  
  14.                 break;  
  15. int flag = a.compareto(b);  
  16. if(flag == -1)  
  17.                 System.out.println("a<b");  
  18. else if(flag == 0)  
  19.                 System.out.println("a==b");  
  20. else  
  21.                 System.out.println("a>b");  
  22.         }  
  23.     }  
  24. }  


大数加法

题目链接http://acm.nyist.net/JudgeOnline/problem.php?pid=103

代码如下:

copy
    import java.math.BigInteger;  
  1. import java.util.*;  
  2. new Scanner(system.in);  
  3. int n = in.nextInt();         
  4. 1; i <= n; ++i)  
  5.         {  
  6.             BigInteger a = in.nextBigInteger();  
  7.             BigInteger b = in.nextBigInteger();  
  8.             BigInteger ans = a.add(b);  
  9.             System.out.println("Case " + i + ":");  
  10.             System.out.println(a + " + " + b + " = " +ans);  
  11.         }  
  12.     }  
  13. }  


递推求值

题目链接http://acm.nyist.net/JudgeOnline/problem.php?pid=114

代码如下:

copy
            BigInteger a[] = new BigInteger[100];  
  1. while(cin.hasNext())  
  2. 0; i <= 2; ++i)  
  3.                 a[i] = cin.nextBigInteger();  
  4. 3; i <= 99; ++i)  
  5.                 a[i] = a[i - 1].add(a[i - 2]).add(a[i - 3]);  
  6.             System.out.println(a[99]);  
  7. }  


高精度幂

题目链接http://acm.nyist.net/JudgeOnline/problem.php?pid=155

代码如下:

copy
    import java.math.BigDecimal;  
  1.             BigDecimal ans = cin.nextBigDecimal();  
  2. int n = cin.nextInt();  
  3.             String res = ans.pow(n).stripTrailingZeros().toPlainString(); //整数去掉小数点和后面的0  
  4. if(res.startsWith("0")) //去掉前导0  
  5.             {  
  6.                 res = res.substring(1);  
  7.             }  
  8.             System.out.println(res);  
  9. }  

相关文章

自1998年我国取消了福利分房的政策后,房地产市场迅速开展蓬...
文章目录获取数据查看数据结构获取数据下载数据可以直接通过...
网上商城系统MySql数据库设计
26个来源的气象数据获取代码
在进入21世纪以来,中国电信业告别了20世纪最后阶段的高速发...