Java大数处理

Java在大数的处理上比C确实流氓多了,在这里我整理一些有关Java的一些基础知识和模板,以便之后学习。

内容大多摘自两位师哥,这里给出原文的链接

---爱国师哥https://www.cnblogs.com/aiguona/p/9133554.html

--福星师哥https://blog.csdn.net/akatsuki__itachi/article/details/81152232

进入到eclipse界面

第一步:file->new->java project->起名->finish

第二步:进入到刚才建的工程里,右键src->new->package->起名->finish

第三步:进入到刚才建的package里,右键name->new->class->起名(这里起名要注意,因为比赛时如果交java代码,这里的类名就要命名为Main,区分大小写

1.Java的输入与输出

Java的输入是先定义一个scanner,然后用这个进行输入,并且每一种输入都有相应的输入函数,具体如下:

 1 public class Main
 2 {
 3     public static void main(String[] args)
 4     {
 5         Scanner cin = new Scanner ( System.in);
 6         int a;
 7         double b;
 8         BigInteger c;
 9         String d;
10         a = cin.nextInt();
11         b = cin.nextDouble();
12         c = cin.nextBigInteger();
13         d = cin.nextLine();
14         // 每种类型都有相应的输入函数.
15     }
16 }

Java的输出

1 System.out.println(a + " " + b);//换行
2 System.out.printf("%d %10.5f
", a, b);
3 
4 /*规格化的输出:
5 函数:这里0指一位数字,#指除0以外的数字(如果是0,则不显示),四舍五入.*/
6 DecimalFormat fd = new DecimalFormat("#.00#");
7 DecimalFormat gd = new DecimalFormat("0.000");
8 System.out.println("x =" + fd.format(x));
9 System.out.println("x =" + gd.format(x));

这里给出几个简单代码

1.输出hellow world

1 package BigInteger;
2 public class Main {
3     public static void main(String args[]) {
4         System.out.println("Hello world");
5     }
6 }

关于上面的package BigInteger,提交代码时不需要粘贴。

2.计算a+b

 1 package BigInteger;
 2 import java.util.*;//导包,相当于c/c++里的头文件
 3 public class Main {
 4     public static void main(String args[]) {
 5         Scanner cin = new Scanner(System.in);
 6         int a,b;
 7         a = cin.nextInt();
 8         b=cin.nextInt();
 9         int ans = a + b;
10         System.out.println(ans);
11     }
12 }

3.多组输入输出

 1 package BigInteger;
 2 import java.util.*;
 3 public class Main {
 4     public static void main(String args[]) {
 5         Scanner cin = new Scanner(System.in);
 6         int a, b;
 7         while (cin.hasNext()) {
 8             a = cin.nextInt();
 9             b = cin.nextInt();
10             int ans = a + b;
11             System.out.println(ans);
12         }
13     }
14 }

2.Java大数处理

下面就开始说大数的相关操作

首先我们需要导包,即BigIntegr类 和 BigDecimal类所在的包

import java,math.*;

*就代表导入包math里面所有的类,如果你不喜欢看到 *

那么你也可以写 import java,math.BigInteger; import java,math.BigDecimal;

1.大整数的加减乘除求余等计算

 1 /*
 2 大数的加减运算不同于普通整数的加减乘除运算
 3 加—— a+b: a=a.add(b);
 4 减—— a-b: a=a.subtract(b); 
 5 乘—— a*b: a=a.multiply(b);
 6 除—— a/b: a=a.divide(b);
 7 求余—a%b: a=a.mod(b);
 8 转换—a=b: b=BigInteger.valueOf(a);
 9 比较 if (ans.compareTo(x) == 0)//比较
10     System.out.println("相等");
11 System.out.println("a + b = "+ans_add); // 这里的‘+’ (第二个) 是连接的意思
12 */
13 package wkf;
14 import java.util.*;
15 import java.math.*;
16 public class Main {
17     public static void main(String args[]) {
18         Scanner cin = new Scanner(System.in);
19         BigInteger a,b,x,y;
20         BigInteger ans_add,ans_sub,ans_mul,ans_div,ans_mod;
21         a=cin.nextBigInteger();
22         b=cin.nextBigInteger();
23         ans_add = a.add(b); //a+b
24         ans_sub = a.subtract(b); //a-b
25         ans_mul = a.multiply(b); //a*b
26         ans_div = a.divide(b); //a/b
27         ans_mod = a.mod(b); //a%b
28         x=BigInteger.valueOf(1);//转换
29         System.out.println("a + b = "+ans_add);
30         System.out.println("a - b = "+ans_sub);
31         System.out.println("a * b = "+ans_mul);
32         System.out.println("a / b = "+ans_div);
33         System.out.println("a % b = " + ans_mod);
34         System.out.println(x);
35         if (a.compareTo(b) == 0)//比较
36             System.out.println("相等");
37         else
38              System.out.println("不相等");
39     }
40 }

3.例题

UVA—10106:a*b

 1 import java.math.BigInteger;
 2 import java.util.*;
 3 public class Main {
 4     public static void main(String args[]) {
 5         Scanner s = new Scanner(System.in);
 6         while(s.hasNext()) {
 7             BigInteger a=s.nextBigInteger();
 8             BigInteger b=s.nextBigInteger();
 9             BigInteger ans=a.multiply(b);
10             System.out.println(ans);
11         }
12     }
13 }

UVA—424:多个数连续相加,遇到0就停止相加,给出结果

 1 import java.math.*;
 2 import java.util.Scanner;
 3 import java.io.*;
 4 public class Main {
 5     public static void main(String args[]){
 6         BigInteger ans = BigInteger.valueOf(0);
 7         BigInteger x;
 8         BigInteger y = BigInteger.valueOf(0);
 9         Scanner cin = new Scanner(System.in);
10         while(cin.hasNext()){
11              x = cin.nextBigInteger();
12              if(x.equals(y)) ///加到0就要停止相加
13                  break;
14              ans  = ans.add(x);
15         }
16         System.out.println(ans);
17     }
18 }

HDU - 1042:计算阶乘:

 1 package wkf;
 2 import java.io.*;
 3 import java.math.BigInteger;
 4 import java.util.*;
 5 public class Main// 注意在oj提交是要用Main
 6 {
 7     public static void main(String[] args) {
 8         Scanner in = new Scanner(System.in);
 9         int n;
10         while (in.hasNext()) {
11             n = in.nextInt();
12             BigInteger sum = BigInteger.valueOf(1);
13             for (int i = 1; i <= n; i++)
14                 sum = sum.multiply(BigInteger.valueOf(i));
15             System.out.println(sum);
16         }
17     }
18 }

UVA —10494

求两个大数相除或者求余

import java.math.BigInteger;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        BigInteger a, b, t = new BigInteger("1");
        String c;
        while (s.hasNext()) {
            a = s.nextBigInteger();
            c = s.next();
            b = s.nextBigInteger();
            if (c.equals("%"))///equals用来比较的是两个对象的内容是否相等
                t = a.mod(b);
            if (c.equals("/"))
                t = a.divide(b);
            System.out.println(t);
        }
    }
}

POJ1001 计算a^b 注意是小数

 1 import java.util.*;
 2 import java.math.*;
 3 
 4 public class Main {
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Scanner input = new Scanner(System.in);
 8         while (input.hasNext()) {
 9             BigDecimal a = input.nextBigDecimal(); // 大数类的double;
10             int b = input.nextInt();
11             a = a.pow(b);
12             String ans = a.stripTrailingZeros().toPlainString(); // 去掉尾部零,转换成非科学计数法字符串
13             if (ans.charAt(0) == '0') { // 如果以0开头
14                 ans = ans.substring(1); // 返回以位置1开头的该字符串
15             }
16             System.out.println(ans);
17         }
18     }
19 }

关于BigDecimal的用法大致上和BigInteger一样。

不过这里需要提一下,在进行大浮点数运算的时候,小数点后面可能会含有多余的后导0

比如0.5000,在题目要求中可能只需要输出0.5

当然,有的题目可能还会要求小数点前面的0也要去掉,输入.5

这时候我们就需要去除掉后导0

转化成 字符型的

方法如下:

1 String str;
2 str=ans.stripTrailingZeros().toPlainString();//去除所有后导0,并且转化成字符型
3 //ans为大浮点数运算后得到的答案
4 //如果小数点前面的0也需要去掉,那么输出的时候处理一下即可:
5 if(str.charAt(0)=='0')//如果以0开头
6         System.out.println(str.substring(1));//返回以位置1开头的字符串
7 else
8         System.out.println(str);

HDU—1023:计算卡特兰数

 1 //卡特兰数递推公式h(n)=h(n-1)*(4*n-2)/(n+1);
 2 import java.math.*;
 3 import java.util.*;
 4 public class Main {
 5   public static void main(String[] args) {
 6       Scanner cin = new Scanner(System.in);
 7       BigInteger dp[];//定义一个数组
 8       dp=new BigInteger[110];//规定数组的大小
 9       dp[1]=BigInteger.valueOf(1);
10       int i,m;
11       for(i=2;i<=100;i++)//卡特兰数打表
12       {
13           dp[i]=dp[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));  
14       }
15       while(cin.hasNext())
16       {
17           m=cin.nextInt();
18           System.out.println(dp[m]);
19       }
20   }
21 }
 
原文地址:https://www.cnblogs.com/wkfvawl/p/9377441.html