今天突然看到了OJ上的大数类题目,由于学习了一点大数类的知识。果断水了6道题。。。。。。都是非常基础的。就当的练手的吧。

学到的只是一些大数类的基本操作。以后多做点这样的题,争取熟练运用水大数题。。。

大数阶乘

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

代码如下:

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

棋盘覆盖

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

代码如下:

  1. import java.math.BigInteger;
  2. import java.util.*;
  3. import java.io.*;
  4. public class Main
  5. {
  6. public static void main(String args[])
  7. {
  8. Scanner in = new Scanner(System.in);
  9. int test = in.nextInt();
  10. while(test-- > 0)
  11. {
  12. int n;
  13. n = in.nextInt();
  14. BigInteger a = new BigInteger("4");
  15. for(int i = 1; i < n; ++i)
  16. a = a.multiply(BigInteger.valueOf(4));
  17. System.out.println(a.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3)));
  18. }
  19. }
  20. }

比较大小

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

代码如下:

  1. import java.io.*;
  2. import java.math.BigInteger;
  3. import java.util.*;
  4. public class Main
  5. {
  6. public static void main(String args[])
  7. {
  8. Scanner cin = 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

代码如下:

  1. import java.math.BigInteger;
  2. import java.util.*;
  3. import java.io.*;
  4. public class Main
  5. {
  6. public static void main(String args[])
  7. {
  8. Scanner in = new Scanner(System.in);
  9. int n = in.nextInt();
  10. for(int i = 1; i <= n; ++i)
  11. {
  12. BigInteger a = in.nextBigInteger();
  13. BigInteger b = in.nextBigInteger();
  14. BigInteger ans = a.add(b);
  15. System.out.println("Case " + i + ":");
  16. System.out.println(a + " + " + b + " = " +ans);
  17. }
  18. }
  19. }

递推求值

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

代码如下:

  1. import java.io.*;
  2. import java.math.BigInteger;
  3. import java.util.*;
  4. public class Main
  5. {
  6. public static void main(String args[])
  7. {
  8. Scanner cin = new Scanner(System.in);
  9. BigInteger a[] = new BigInteger[100];
  10. while(cin.hasNext())
  11. {
  12. for(int i = 0; i <= 2; ++i)
  13. a[i] = cin.nextBigInteger();
  14. for(int i = 3; i <= 99; ++i)
  15. a[i] = a[i - 1].add(a[i - 2]).add(a[i - 3]);
  16. System.out.println(a[99]);
  17. }
  18. }
  19. }

高精度幂

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

代码如下:

  1. import java.io.*;
  2. import java.math.BigDecimal;
  3. import java.util.*;
  4. public class Main
  5. {
  6. public static void main(String args[])
  7. {
  8. Scanner cin = new Scanner(System.in);
  9. while(cin.hasNext())
  10. {
  11. BigDecimal ans = cin.nextBigDecimal();
  12. int n = cin.nextInt();
  13. String res = ans.pow(n).stripTrailingZeros().toPlainString(); //整数去掉小数点和后面的0
  14. if(res.startsWith("0")) //去掉前导0
  15. {
  16. res = res.substring(1);
  17. }
  18. System.out.println(res);
  19. }
  20. }
  21. }

JAVA大数类练手的更多相关文章

  1. JAVA大数类

    JAVA大数类api http://man.ddvip.com/program/java_api_zh/java/math/BigInteger.html#method_summary 不仅仅只能查J ...

  2. ZOJ3477&JAVA大数类

    转:http://blog.csdn.net/sunkun2013/article/details/11822927 import java.util.*; import java.math.BigI ...

  3. Java大数类介绍

    java能处理大数的类有两个高精度大整数BigInteger 和高精度浮点数BigDecimal,这两个类位于java.math包内,要使用它们必须在类前面引用该包:import java.math. ...

  4. HDU高精度总结(java大数类)

      HDU1002   A + B Problem II [题意]大数相加 [链接]http://acm.hdu.edu.cn/showproblem.php?pid=1002 Sample Inpu ...

  5. JAVA大数类—基础操作(加减乘除、取模、四舍五入、设置保留位数)

    当基础数据类型长度无法满足需求时可以使用大数类 构造方法接受字符串为参数 BigInteger bInt = new BigInteger("123123"); BigDecima ...

  6. JAVA - 大数类详解

    写在前面 对于ACMer来说,java语言最大的优势就是BigInteger,Bigdecimal,String三个类. 这三个类分别是高精度整数,高精度浮点数和字符串,之所以说这个是它的优势是因为j ...

  7. Java 大数类BigInteger和BigDecimal的基本函数

    在Java中有两个类BigInteger和BigDecimal分别表示不可变的任意精度的整数和不可变的有符号的任意精度的十进制数(浮点数).主要用于高精度计算中.这两个类使得java中的大数,高精度运 ...

  8. Java大数类BigDecimal及八种舍入模式的介绍

    BigDecimal的引入 在利用Java编程语言开发银行.金融类等需要对数值进行高精度计算的软件时,我们经常使用BigDecimal和BigInteger这两个大数据类,而不是常见的int.long ...

  9. Java 大数类

    划分结果存在数组.供应商下标0 在剩下的标记1 import java.math.BigInteger; import java.util.Scanner; public class Main { p ...

随机推荐

  1. 搭建高可用的MongoDB集群

    http://www.csdn.net/article/2014-04-09/2819221-build-high-avialable-mongodb-cluster-part-1/1 在大数据的时代 ...

  2. 20160510--hibernate懒加载问题

    懒加载 通过asm和cglib二个包实现:Domain是非final的. 1.session.load懒加载. 2.one-to-one(元素)懒加载: 必需同时满足下面三个条件时才能实现懒加载 (主 ...

  3. JavaScript解析json

    后台数据经常以json数据格式传回前台,解析当然首选JSON对象. JSON对象有两个方法,使用JSON.parse(str)可以将json字符串解析成js中的对象. var o = JSON.par ...

  4. Mac:How to mount a Windows shared folder

    Reference: How to mount a Windows shared folder on your Mac

  5. Linux内核中的通用双向循环链表

    开发中接触Linux越来越多,休息放松之余,免不了翻看翻看神秘的Linux的内核.看到双向链表时,觉得挺有意思的,此文记下. 作为众多基础数据结构中的一员,双向循环链表在各种“教科书”中的实现是相当的 ...

  6. free -m

    free -m total used free shared buffers cached Mem: 7760 1572   6187          0              9       ...

  7. htmlt中的块状元素与内联元素

    块元素(block element) address - 地址 blockquote - 块引用 center - 举中对齐块 dir - 目录列表 div - 常用块级容易,也是CSS layout ...

  8. Cassandra1.2文档学习(5)—— Snitch

    参考资料:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/a ...

  9. android通过泛型获取控件或视图

    @SuppressWarnings("unchecked") public <T extends Fragment> T getFragment(int id) { T ...

  10. codeforces edu round3

    B. The Best Gift  传送门:http://codeforces.com/problemset/problem/609/B Emily's birthday is next week a ...