Java大数问题

以前就听说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来行代码就解决了!!!

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.nyist;  
  2.   
  3. import java.math.BigInteger;  
  4. import java.util.Scanner;  
  5.   
  6. public class nyist103 {  
  7.   
  8.     public static void main(String[] args) {  
  9.         Scanner scanf=new Scanner(System.in);  
  10.         int t=scanf.nextInt();  
  11.       for(int i=1;i<=t;i++)  
  12.       {  
  13.           BigInteger a=scanf.nextBigInteger();  
  14.           BigInteger b=scanf.nextBigInteger();  
  15.           BigInteger sum=a.add(b);    
  16.           System.out.println("Case " + i + ":");    
  17.           System.out.println(a + " + " + b + " = " +sum);    
  18.       }  
  19.     }  
  20.   
  21. }  
  22. /*add 
  23. public BigInteger add(BigInteger val)返回其值为 (this + val) 的 BigInteger。  
  24.  
  25. 参数: 
  26. val - 将添加到此 BigInteger 中的值。  
  27. 返回: 
  28. this + val 
  29. */  

java中就是要掌握这些类和方法的使用

以前用c语言写的这道题,(差不多算自己写的大数加法的模板吧)

 

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.    
  2. #include<stdio.h>  
  3. #include<string.h>  
  4. int main()  
  5. {  
  6.     int t,i,j,a[1001],b[1001],c[1001],lenth1,lenth2,n,m=1;  
  7.     char s1[1001],s2[1001];  
  8.     scanf("%d",&t);  
  9.     while(t--)  
  10.     {  
  11.         n=0;  
  12.         memset(a,0,sizeof(a));  
  13.         memset(b,0,sizeof(b));  
  14.         memset(c,0,sizeof(c));  
  15.         scanf("%s%s",s1,s2);  
  16.         lenth1=strlen(s1);  
  17.         lenth2=strlen(s2);  
  18.         n=(lenth1>lenth2)?lenth1:lenth2;  
  19.         for(j=0,i=lenth1-1;i>=0;i--)  
  20.             a[j++]=s1[i]-'0';  
  21.         for(j=0,i=lenth2-1;i>=0;i--)  
  22.             b[j++]=s2[i]-'0';  
  23.         for(i=0;i<n;i++)  
  24.         {  
  25.             c[i]+=a[i]+b[i];  
  26.             if(c[i]>=10)//进位  
  27.             {  
  28.                 c[i+1]=c[i]/10;  
  29.                 c[i]%=10;  
  30.             }  
  31.         }  
  32.         printf("Case %d:\n",m++);  
  33.      printf("%s + %s = ",s1,s2);  
  34.     while(n>=0 && !c[n]) n--;        //去前导零  
  35.     while(n>=0) printf("%d", c[n--]);    //输出  
  36.     printf("\n");  
  37.  }  
  38.  return 0;  
  39. }          

大数阶乘

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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.nyist;  
  2.   
  3. import java.io.*;  
  4. import java.math.BigInteger;  
  5. import java.util.Scanner;  
  6.   
  7. public class Main {  
  8.     public static void main(String[] args) {  
  9.         Scanner cin=new Scanner(System.in);  
  10.          int m=cin.nextInt();  
  11.          BigInteger ans=BigInteger.ONE;  
  12.          for(int i=1;i<=m;i++)  
  13.          ans=ans.multiply(BigInteger.valueOf(i));  
  14.        System.out.println(ans);  
  15. }  
  16. }  
  17. /*valueOf 
  18. public static BigInteger valueOf(long val)返回其值等于指定 long 的值的 BigInteger。提供的此“静态工厂方法”优先于 (long) 构造方法,因为前者允许重用经常使用的 BigInteger。  
  19.  
  20. 参数: 
  21. val - 要返回的 BigInteger 的值。  
  22. 返回: 
  23. 具有指定值的 BigInteger。*/  
  24. /*multiply 
  25. public BigInteger multiply(BigInteger val)返回其值为 (this * val) 的 BigInteger。  
  26.  
  27. 参数: 
  28. val - 要乘以此 BigInteger 的值。  
  29. 返回: 
  30. this * val 
  31. */  

大数除法

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

下面是ac的代码
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.nyist;  
  2.   
  3. import java.math.BigInteger;  
  4. import java.util.Scanner;  
  5.   
  6. public class nyist803 {  
  7.     
  8.     public static void main(String[] args) {  
  9.         Scanner cin=new Scanner(System.in);  
  10.         BigInteger a,b;  
  11.         String s;  
  12.         int i;  
  13.         while(cin.hasNext())  
  14.         {  
  15.             a=cin.nextBigInteger();  
  16.             s=cin.next();  
  17.             b=cin.nextBigInteger();  
  18.            /* for(i=0;i<s.length();i++) //先前看到别人是这么写的,感觉没后面那种好用 
  19.             { 
  20.                 if(s.charAt(i)=='/') break; 
  21.             } 
  22.             if(i<s.length()) 
  23.             { 
  24.                 System.out.println(a.divide(b)); 
  25.             } 
  26.             else 
  27.             { 
  28.                 System.out.println(a.mod(b)); 
  29.         } 
  30.             */  
  31.             if(s.equals("/"))//这种写法还是比较简洁,先前用了==号,这里不能用==  
  32.             {  
  33.                 System.out.println(a.divide(b));  
  34.             }  
  35.             else  
  36.             {  
  37.                 System.out.println(a.mod(b));  
  38.             }  
  39.     }  
  40.   
  41. }  
  42. }  



比较大小

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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.nyist;  
  2.   
  3. import java.math.BigInteger;  
  4. import java.util.Scanner;  
  5.   
  6. public class nyist73 {  
  7.   
  8.     public static void main(String[] args) {  
  9.       Scanner scanf=new Scanner(System.in);  
  10.     while(scanf.hasNext())  
  11.     {  
  12.         BigInteger a=scanf.nextBigInteger();  
  13.         BigInteger b=scanf.nextBigInteger();  
  14.         if(a.equals(BigInteger.ZERO)&&b.equals(BigInteger.ZERO))  
  15.             break;  
  16.         int flag=a.compareTo(b);  
  17.         if(flag==-1)  
  18.             System.out.println("a<b");  
  19.         else if(flag==0)  
  20.             System.out.println("a==b");  
  21.         else   
  22.             System.out.println("a>b");  
  23.     }  
  24.     }  
  25. }  
  26.   
  27. /*compareTo 
  28. public int compareTo(BigInteger val)将此 BigInteger 与指定的 BigInteger 进行比较。对于针对六个布尔比较运算符 (<, ==, >, >=, !=, <=) 中的每一个运算符的各个方法,优先提供此方法。执行这些比较的建议语句是:(x.compareTo(y) <op> 0),其中 <op> 是六个比较运算符之一。  
  29.  
  30. 指定者: 
  31. 接口 Comparable<BigInteger> 中的 compareTo 
  32. 参数: 
  33. val - 将此 BigInteger 与之比较的 BigInteger。  
  34. 返回: 
  35. 当此 BigInteger 在数值上小于、等于或大于 val 时,返回 -1,0,或 1。 
  36. */  

棋盘覆盖

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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.nyist;  
  2.   
  3. import java.io.*;  
  4. import java.math.BigInteger;  
  5. import java.util.Scanner;  
  6.   
  7. public class nyist45 {  
  8.   
  9.     public static void main(String[] args) {  
  10.           Scanner scanf=new Scanner(System.in);  
  11.           int m=scanf.nextInt();  
  12.           while(m-->0)  
  13.           {  
  14.               int k;  
  15.               k=scanf.nextInt();  
  16.               BigInteger a=new BigInteger("4");  
  17.               for(int i=1;i<k;i++)  
  18.                   a=a.multiply(BigInteger.valueOf(4));  
  19.               System.out.println(a.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3)));  
  20.           }  
  21.     }  
  22. }  
  23. /*subtract 
  24. public BigInteger subtract(BigInteger val)返回其值为 (this - val) 的 BigInteger。  
  25.  
  26. 参数: 
  27. val - 从此 BigInteger 中减去的值。  
  28. 返回: 
  29. this - val 
  30. */  
  31.   
  32. /*divide 
  33. public BigInteger divide(BigInteger val)返回其值为 (this / val) 的 BigInteger。  
  34.  
  35. 参数: 
  36. val - 此 BigInteger 要除以的值。  
  37. 返回: 
  38. this / val  
  39. 抛出:  
  40. ArithmeticException - val==0 
  41. */  

递推求值

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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.nyist;  
  2.   
  3. import java.math.BigInteger;  
  4. import java.util.Scanner;  
  5.   
  6. public class nyist114 {  
  7.   
  8.     public static void main(String[] args) {  
  9.         Scanner scanf=new Scanner(System.in);  
  10.         BigInteger a[]=new BigInteger[100];  
  11.         while(scanf.hasNext())  
  12.         {  
  13.             for(int i=0;i<=2;i++)  
  14.                 a[i]=scanf.nextBigInteger();  
  15.             for(int i=3;i<=99;i++)  
  16.                 a[i]=a[i-1].add(a[i-2]).add(a[i-3]);  
  17.             System.out.println(a[99]);  
  18.         }  
  19.     }  
  20.   
  21. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值