微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

大数模板

大数阶乘

题目链接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. }  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐