各种基本操作

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

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

加法(斐波那契)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

乘法(阶乘)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

标签:BigInteger,java,Scanner,大数,四则运算,new,import,public

来源: https://www.cnblogs.com/pangbi/p/13862946.html

Logo

昇腾计算产业是基于昇腾系列(HUAWEI Ascend)处理器和基础软件构建的全栈 AI计算基础设施、行业应用及服务,https://devpress.csdn.net/organization/setting/general/146749包括昇腾系列处理器、系列硬件、CANN、AI计算框架、应用使能、开发工具链、管理运维工具、行业应用及服务等全产业链

更多推荐