以前就听说Java处理大数类问题很轻松,今天就针对这类问题单独练习了一下,代码真心的好简洁啊!!(但是感觉java语言的效率真心的不如c/c++语言)但是大数类问题的代码太简洁了啊,用c差不多100行,java就是那么20行就解决了!!
参考了大神的博客http://blog.csdn.net/niushuai666/article/details/6972991
java大数类根本停不下来啊,一口气做了5个;
大数加法
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=103
这又算是对大数问题的一次复习吧,以前用C语言写了好久,搞了好几天,java就20来行代码就解决了!!!
- package com.nyist;
- import java.math.BigInteger;
- import java.util.Scanner;
- public class nyist103 {
- public static void main(String[] args) {
- Scanner scanf=new Scanner(System.in);
- int t=scanf.nextInt();
- for(int i=1;i<=t;i++)
- {
- BigInteger a=scanf.nextBigInteger();
- BigInteger b=scanf.nextBigInteger();
- BigInteger sum=a.add(b);
- System.out.println("Case " + i + ":");
- System.out.println(a + " + " + b + " = " +sum);
- }
- }
- }
- /*add
- public BigInteger add(BigInteger val)返回其值为 (this + val) 的 BigInteger。
- 参数:
- val - 将添加到此 BigInteger 中的值。
- 返回:
- this + val
- */
java中就是要掌握这些类和方法的使用
以前用c语言写的这道题,(差不多算自己写的大数加法的模板吧)
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- int t,i,j,a[1001],b[1001],c[1001],lenth1,lenth2,n,m=1;
- char s1[1001],s2[1001];
- scanf("%d",&t);
- while(t--)
- {
- n=0;
- memset(a,0,sizeof(a));
- memset(b,0,sizeof(b));
- memset(c,0,sizeof(c));
- scanf("%s%s",s1,s2);
- lenth1=strlen(s1);
- lenth2=strlen(s2);
- n=(lenth1>lenth2)?lenth1:lenth2;
- for(j=0,i=lenth1-1;i>=0;i--)
- a[j++]=s1[i]-'0';
- for(j=0,i=lenth2-1;i>=0;i--)
- b[j++]=s2[i]-'0';
- for(i=0;i<n;i++)
- {
- c[i]+=a[i]+b[i];
- if(c[i]>=10)//进位
- {
- c[i+1]=c[i]/10;
- c[i]%=10;
- }
- }
- printf("Case %d:\n",m++);
- printf("%s + %s = ",s1,s2);
- while(n>=0 && !c[n]) n--; //去前导零
- while(n>=0) printf("%d", c[n--]); //输出
- printf("\n");
- }
- return 0;
- }
大数阶乘
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=28
- package com.nyist;
- import java.io.*;
- import java.math.BigInteger;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner cin=new Scanner(System.in);
- int m=cin.nextInt();
- BigInteger ans=BigInteger.ONE;
- for(int i=1;i<=m;i++)
- ans=ans.multiply(BigInteger.valueOf(i));
- System.out.println(ans);
- }
- }
- /*valueOf
- public static BigInteger valueOf(long val)返回其值等于指定 long 的值的 BigInteger。提供的此“静态工厂方法”优先于 (long) 构造方法,因为前者允许重用经常使用的 BigInteger。
- 参数:
- val - 要返回的 BigInteger 的值。
- 返回:
- 具有指定值的 BigInteger。*/
- /*multiply
- public BigInteger multiply(BigInteger val)返回其值为 (this * val) 的 BigInteger。
- 参数:
- val - 要乘以此 BigInteger 的值。
- 返回:
- this * val
- */
大数除法
题目链接http://acm.nyist.net/JudgeOnline/problem.php?pid=803
下面是ac的代码
- package com.nyist;
- import java.math.BigInteger;
- import java.util.Scanner;
- public class nyist803 {
- public static void main(String[] args) {
- Scanner cin=new Scanner(System.in);
- BigInteger a,b;
- String s;
- int i;
- while(cin.hasNext())
- {
- a=cin.nextBigInteger();
- s=cin.next();
- b=cin.nextBigInteger();
- /* for(i=0;i<s.length();i++) //先前看到别人是这么写的,感觉没后面那种好用
- {
- if(s.charAt(i)=='/') break;
- }
- if(i<s.length())
- {
- System.out.println(a.divide(b));
- }
- else
- {
- System.out.println(a.mod(b));
- }
- */
- if(s.equals("/"))//这种写法还是比较简洁,先前用了==号,这里不能用==
- {
- System.out.println(a.divide(b));
- }
- else
- {
- System.out.println(a.mod(b));
- }
- }
- }
- }
比较大小
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=73
- package com.nyist;
- import java.math.BigInteger;
- import java.util.Scanner;
- public class nyist73 {
- public static void main(String[] args) {
- Scanner scanf=new Scanner(System.in);
- while(scanf.hasNext())
- {
- BigInteger a=scanf.nextBigInteger();
- BigInteger b=scanf.nextBigInteger();
- if(a.equals(BigInteger.ZERO)&&b.equals(BigInteger.ZERO))
- break;
- int flag=a.compareTo(b);
- if(flag==-1)
- System.out.println("a<b");
- else if(flag==0)
- System.out.println("a==b");
- else
- System.out.println("a>b");
- }
- }
- }
- /*compareTo
- public int compareTo(BigInteger val)将此 BigInteger 与指定的 BigInteger 进行比较。对于针对六个布尔比较运算符 (<, ==, >, >=, !=, <=) 中的每一个运算符的各个方法,优先提供此方法。执行这些比较的建议语句是:(x.compareTo(y) <op> 0),其中 <op> 是六个比较运算符之一。
- 指定者:
- 接口 Comparable<BigInteger> 中的 compareTo
- 参数:
- val - 将此 BigInteger 与之比较的 BigInteger。
- 返回:
- 当此 BigInteger 在数值上小于、等于或大于 val 时,返回 -1,0,或 1。
- */
棋盘覆盖
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=45
- package com.nyist;
- import java.io.*;
- import java.math.BigInteger;
- import java.util.Scanner;
- public class nyist45 {
- public static void main(String[] args) {
- Scanner scanf=new Scanner(System.in);
- int m=scanf.nextInt();
- while(m-->0)
- {
- int k;
- k=scanf.nextInt();
- BigInteger a=new BigInteger("4");
- for(int i=1;i<k;i++)
- a=a.multiply(BigInteger.valueOf(4));
- System.out.println(a.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3)));
- }
- }
- }
- /*subtract
- public BigInteger subtract(BigInteger val)返回其值为 (this - val) 的 BigInteger。
- 参数:
- val - 从此 BigInteger 中减去的值。
- 返回:
- this - val
- */
- /*divide
- public BigInteger divide(BigInteger val)返回其值为 (this / val) 的 BigInteger。
- 参数:
- val - 此 BigInteger 要除以的值。
- 返回:
- this / val
- 抛出:
- ArithmeticException - val==0
- */
递推求值
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=114
- package com.nyist;
- import java.math.BigInteger;
- import java.util.Scanner;
- public class nyist114 {
- public static void main(String[] args) {
- Scanner scanf=new Scanner(System.in);
- BigInteger a[]=new BigInteger[100];
- while(scanf.hasNext())
- {
- for(int i=0;i<=2;i++)
- a[i]=scanf.nextBigInteger();
- for(int i=3;i<=99;i++)
- a[i]=a[i-1].add(a[i-2]).add(a[i-3]);
- System.out.println(a[99]);
- }
- }
- }