java中的大数类 下载本文

内容发布更新时间 : 2024/4/16 23:56:02星期一 下面是文章的全部内容请认真阅读。

大数用C或C++来写,用数组模拟操作很复杂,那么有没有一个专门的类-----大数类来帮助我们实现大数操作呢?

答案是肯定的,有,java有大数类操作,java是大三才学的,但是就像没学C++以前,一些字符串的操作用C语言还得自己写代码,学过C++之后,你会发现,调用一些现成的函数,你会发现那些所谓的操作很好实现。

Java大数类也是一样,当然,你会C或C++的模拟操作,觉得不用学习java,现在不学也行,但是早晚都得学。

平顶山学院OJ不支持java提交代码,我试过,A+B都过不了,现在先介绍java的一些操作,特别是大数类的操作,然后以南阳理工的大数分类题目讲解一下代码。 一, java的头文件

类似C或C++的 # include 包含头文件

Java也有相应的头文件,在java中不叫头文件,java中叫包。

import java.io.* 通过数据流、序列化和文件系统提供系统输入和输出。 importjava.util.* 我们所用的输入scanner在这个包中。

importjava.math.* 我们下面要用到的BigInteger就这这个包中。 二, java中的输入输出

读入 Scanner cin=new Scanner(System.in)

While(cin.hasNext()) //相当于C语言中的!=EOF n=cin.nextInt(); //输入一个int型整数 n=cin.nextBigInteger(); //输入一个大整数 System.out.print(n); //输出n但不换行 System.out.println(); //换行

System.out.println(n); //输出n并换行

System.out.printf(“%d\\n”,n); //类似C语言中的输出 三, 定义变量

定义单个变量:

int a,b,c; //和C++ 中无区别 。 BigInteger a; //定义大数变量a 。

BigIntegerb= new BigInteger(\定义大数变量 b赋值为 2。 BigDecimaln; //定义大浮点数类 n。 定于数组:

int a[]= new int[10] //定义长度为10的数组a 。

BigInteger b[] =new BigInteger[100] //定义长度为100的大数数组b。 四, 大数的表示范围

BigInteger任意大的数,原则上只要你的计算机内存足够大,可以有无限位。 五, 一些终止程序的操作

1,相当于C语言的EOF

Java代码:While(cin.hasNext()) 2,以 0 0结束 Java代码:

BigInteger a = cin.nextBigInteger(); BigInteger b = cin.nextBigInteger();

if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO)) break;

六, 大数的一些操作

A=BigInteger.ONE; //把0赋给A

B=BigInteger. valueOf (3); //把3赋给B; A[i]=BigInteger. valueOf (10); //把10赋给A[i] c=a.add(b) //把a与b相加并赋给c c=a.subtract(b) //把a与b相减并赋给c c=a.multiply(b) //把a与b相乘并赋给c c=a.divide(b) //把a与b相除并赋给c c=a.mod(b) // 相当于a%b a.pow(b) //相当于a^b

a.compareTo(b): //根据该数值是小于、等于、或大于a 返回 -1、0 或 1; a.equals(b): //判断两数是否相等,也可以用compareTo来代替; a.min(b),a.max(b): //取两个数的较小、大者; 注意以上的操作都必须是BigInteger类的。 七, 对应的练习题。

1, 大数阶乘

题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=28 import java.io.*;//输入输出

import java.math.BigInteger;//java中的大数类 import java.util.*;//Scanner在这个包中

public class Main //类 学过C++的应该知道,C++和java都是面向对象的语言 { public static void main(String args[]) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); BigInteger ans = BigInteger.ONE;//ans=0 for(int i = 1; i <= n; ++i) ans = ans.multiply(BigInteger.valueOf(i));//i的值赋给括号里面 System.out.println(ans); } } 2, 棋盘覆盖

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=45 题目思路:一道大数规律题4^0+4^1+4^2+4^3+……+4^n-1 import java.math.*; import java.util.Scanner; public class Main {

public static void main(String[] args) { int i;

BigInteger s=new BigInteger(\

Scanner cin=new Scanner(System.in); int n=cin.nextInt(); for(i=0;i

BigInteger sum = new BigInteger(\int m=cin.nextInt(); for(int j=1;j

sum=sum.add(s.pow(j)); }

System.out.println(sum); }

} }

3,比大小

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=73 import java.io.*;

import java.math.BigInteger; import java.util.*; public class Main {

public static void main(String args[]) { Scanner cin = new Scanner(System.in); while(cin.hasNext()) { BigInteger a = cin.nextBigInteger(); BigInteger b = cin.nextBigInteger(); if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO)) break; int flag = a.compareTo(b); if(flag == -1) System.out.println(\ else if(flag == 0) System.out.println(\ else System.out.println(\ } } }

4, A+B Problem II

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=103 import java.math.BigInteger; import java.util.*;