java写的大数算法
2008-04-27 22:19
351 查看
用例子阐述:
例题:
nPOJ 1503 Integer Inquiry
–Description
省略
–Input
The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).
The final input line will contain a single zero on a line by itself.
–Output
Your program should output the sum of the VeryLongIntegers given in the input.
–Sample Input
•123456789012345678901234567890
•123456789012345678901234567890
•123456789012345678901234567890
•0
–Sample Output
•370370367037037036703703703670
题解:
import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main {
public static void main(String[] args){
BigInteger sum=new BigInteger("0");
BigInteger num;
Scanner cin=new Scanner(System.in);
for(;;)
{
num=cin.nextBigInteger();
if(num.toString()=="0")break;
sum=sum.add(num);
}
System.out.println(sum.toString());
}
}
补方法如下:
add
返回一个 BigInteger ,其值是 (this + val) 。
subtract
返回一个 BigInteger ,其值是 (this - val) 。
multiply
返回一个 BigInteger ,其值是 (this * val) 。
divide
返回一个 BigInteger ,其值是 (this / val) 。 如果 val == 0 ,则抛出 ArithmeticException 。
remainder
返回一个 BigInteger ,其值是 (this % val) 。 如果 val == 0,则抛出 ArithmeticException 。
hobo 2008-04-27 22:19 发表评论
例题:
nPOJ 1503 Integer Inquiry
–Description
省略
–Input
The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).
The final input line will contain a single zero on a line by itself.
–Output
Your program should output the sum of the VeryLongIntegers given in the input.
–Sample Input
•123456789012345678901234567890
•123456789012345678901234567890
•123456789012345678901234567890
•0
–Sample Output
•370370367037037036703703703670
题解:
import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main {
public static void main(String[] args){
BigInteger sum=new BigInteger("0");
BigInteger num;
Scanner cin=new Scanner(System.in);
for(;;)
{
num=cin.nextBigInteger();
if(num.toString()=="0")break;
sum=sum.add(num);
}
System.out.println(sum.toString());
}
}
补方法如下:
add
public BigInteger add(BigInteger val) throws ArithmeticException
返回一个 BigInteger ,其值是 (this + val) 。
subtract
public BigInteger subtract(BigInteger val)
返回一个 BigInteger ,其值是 (this - val) 。
multiply
public BigInteger multiply(BigInteger val)
返回一个 BigInteger ,其值是 (this * val) 。
divide
public BigInteger divide(BigInteger val) throws ArithmeticException
返回一个 BigInteger ,其值是 (this / val) 。 如果 val == 0 ,则抛出 ArithmeticException 。
remainder
public BigInteger remainder(BigInteger val) throws ArithmeticException
返回一个 BigInteger ,其值是 (this % val) 。 如果 val == 0,则抛出 ArithmeticException 。
hobo 2008-04-27 22:19 发表评论
相关文章推荐
- 大数算法 -- 正整数相乘(Java 实现)
- 大数相乘问题--算法思想及Java实现解析(附详细注释)
- java大数算法完成五则运算之类基础
- java大数算法完成五则运算之除及取余
- 牛客网NowCoder 2018年全国多校算法寒假训练营练习比赛(第三场)A.不凡的夫夫(斯特林公式) D.小牛vs小客 E.进击吧!阶乘(大数Java) G.大水题(数学)
- Java大数算法,真的超级 方便啊
- java大数算法完成五则运算之乘
- 2018年全国多校算法寒假训练营练习比赛(第三场)---E---题(大数阶乘(模板题)c++/java/py)
- 大数手算法开平方(Java实现)
- java大数算法完成五则运算之加减
- 2018年全国多校算法寒假训练营练习比赛(第三场)E---进击吧!阶乘(Java代码,另附Java解决大数问题的模板)
- 大数相加,相减,相乘算法(JAVA代码)
- 算法收藏(stl 背包 并查集 矩阵乘法 java大数)
- java大数相乘算法
- java实现大数减法算法
- java大数模板
- java常用算法之螺旋矩阵
- 探讨大数阶乘的算法
- JAVA蓝桥杯:高精度算法
- java 大数加法