java大数四则运算

各种基本操作

 1 import java.math.BigInteger;
 2 import java.util.*;
 3 public class Main{
 4     public static void main(String[] args)
 5     {
 6          Scanner input = new Scanner(System.in);
 7          BigInteger a = input.nextBigInteger();//输入一个大数
 8          BigInteger b = BigInteger.valueOf(0);//初始化一个为0的大数
 9          BigInteger c = BigInteger.valueOf(1);//初始化一个为1的大数
10          BigInteger []shu = new BigInteger[4040];//初始化一个4040的大数数组
11          int I = 1234567;
12          BigInteger d = BigInteger.valueOf(I);//将int转化成BigInt
13          String s = "1234567";
14          BigInteger f = new BigInteger(s);    //将String转为BigInt
15          int e = 10010;
16          a = a.add(b);        //a = a+b
17          a = a.subtract(b); //a = a-b
18          a = a.divide(b);   //a = a/b
19          a = a.multiply(b); //a = a*b
20          a = a.mod(b);      //a = a%b
21          if(a.equals(a.max(b)));// if(a>b)
22          if(b.equals(a.max(b)));// if(a<b)
23          
24          c = a.pow(e);      //a = a^e  e必须为int类型
25          c = a.gcd(b);      //a和b的最大公约数
26          
27          int len = a.toString().length(); //求大数的长度
28          while(input.hasNext())//一直输入
29          {
30              BigInteger k = input.nextBigInteger();
31          }
32     }
33 }
View Code

 

加法(斐波那契)

 1 import java.io.*;
 2 import java.util.*;
 3 import java.math.*;
 4 
 5 
 6 public class Main {
 7     public static void main(String[] args) {
 8         Scanner cin = new Scanner(System.in);
 9         BigInteger []fic = new BigInteger[1010];
10         fic[0]=fic[1]=BigInteger.ONE;
11         for(int i=2;i<=1000;i++){
12             fic[i]=fic[i-1].add(fic[i-2]);
13         }
14         int T = cin.nextInt();
15         for(int i=1;i<=T;i++){
16             int tmp=cin.nextInt();
17             System.out.println(fic[tmp]);
18         }
19     }
20 
21 }
View Code

乘法(阶乘)

 1 import java.io.*;
 2 import java.util.*;
 3 import java.math.*;
 4 
 5 
 6 public class Main {
 7     public static void main(String[] args) {
 8         Scanner cin = new Scanner(System.in);
 9         BigInteger []jc = new BigInteger[1010];
10         jc[1]=BigInteger.ONE;
11         for(int i=2;i<=1000;i++){
12             jc[i]=jc[i-1].multiply(BigInteger.valueOf(i));
13         }
14         int T = cin.nextInt();
15         for(int i=1;i<=T;i++){
16             int tmp=cin.nextInt();
17             System.out.println(jc[tmp]);
18         }
19     }
20 
21 }
View Code

 

posted @ 2020-10-23 10:59  古比  阅读(168)  评论(0编辑  收藏  举报