如何存下俩个大数相乘的结果?急

zeer527 2008-12-31 10:18:49
Java语言 两个大数相乘比如1234567890123×1234567890123 结果肯定任何类型都存不下 会溢出的 ,要怎么解决这个问题呢?
有兄弟提示说用移位运算效率高能解决,期待解决的具体思路,及源码 ,谢谢啦
...全文
444 26 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
paodan 2009-03-24
  • 打赏
  • 举报
回复
存在链表中
fanyuanwaifdl 2009-01-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ssqtjffcu1 的回复:]
呵呵,都被楼上的答完了.楼上的都正确.
用java.math.BigInteger,java.math.Decimal
[/Quote]
up
酒剑仙 2009-01-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ssqtjffcu1 的回复:]
呵呵,都被楼上的答完了.楼上的都正确.
用java.math.BigInteger,java.math.Decimal
[/Quote]
学习了
talent_marquis 2009-01-09
  • 打赏
  • 举报
回复
标记学习了!
likgui 2009-01-08
  • 打赏
  • 举报
回复
学习了
小刚asdf 2009-01-08
  • 打赏
  • 举报
回复
学习,体验高手的代码!!
qusic 2009-01-08
  • 打赏
  • 举报
回复
呵呵,类库和代码的实现都有了啊~
Alien 2009-01-08
  • 打赏
  • 举报
回复
当然是用BigInteger或BigDecimal之类的了,都是集成的工具类,很多方法可以用的
Buddha_sy 2009-01-05
  • 打赏
  • 举报
回复
学习了
sj2love0qh 2009-01-05
  • 打赏
  • 举报
回复
learn!
xql80329 2009-01-04
  • 打赏
  • 举报
回复
2 3说了
languo 2009-01-04
  • 打赏
  • 举报
回复
mark
忙碌的布谷鸟 2009-01-04
  • 打赏
  • 举报
回复

// =============== Program Description ===============
// 程序名称: ConsoleReader.java
// 程序目的: 本类用于数据的输入及转换。
// ===================================================
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;

public class ConsoleReader
{
public ConsoleReader(InputStream inStream)
{
reader = new BufferedReader (new InputStreamReader(inStream));
}

public int readInt() // 整数输入
{
String inputString = readLine();
int n = Integer.parseInt(inputString);

return n;
}

public double readDouble() // 浮点数输入
{
String inputString = readLine();
double x = Double.parseDouble(inputString);

return x;
}

public String readLine()
{
String inputLine = "";

try
{
inputLine = reader.readLine();
}
catch(IOException e)
{
System.out.println(e);
System.exit(1);
}

return inputLine;
}

private BufferedReader reader;
}
忙碌的布谷鸟 2009-01-04
  • 打赏
  • 举报
回复

本例仅供参考。
思想是最好的,将每位数在数组中单独存放。

// =============== Program Description ===============
// 程序名称: array04.java
// 程序目的: 设计一个可容纳40位数的求n!程序
// ===================================================
import ConsoleReader.*; // 导入已定义的数据输入类

public class array04
{
public static void main(String args[])
{

int Data[] = new int[40]; // 储存40位数的整数数组
int Digit; // 数据位数变量
int i,j,r,k; // 回圈计数变量
int N; // 用户输入值

for (i=1;i<40;i++) // 将数组初始值设为0
Data[i] = 0;

Data[0] = 1; // 设数组第0位数为1
Data[1] = 1; // 设数组第1位数为1
Digit = 1; // 设数据位数为1

System.out.print("Enter a number what you want to calculus : ");
ConsoleReader console = new ConsoleReader(System.in);
// 读取用户欲求的N值
N = console.readInt();

for (i=1;i<N+1;i++)
{
for (j=1;j<Digit+1;j++)
Data[j] *= i; // 数组中内容的运算
for (j=1;j<Digit+1;j++)
{
if (Data[j]>10)
{
for (r=1;r<Digit+1;r++)
{
if (Data[Digit]>10)
Digit++;
// 当数组中的值大于10时,则位数加1
Data[r+1] += Data[r]/10;
// 前一位数组值 = 前一位数组值 + 此位数组值除以10
Data[r]=Data[r]%10;
// 此位数组值 = 此位数组值除10取余数
}
}
}
System.out.print(i+"! = ");
for (k=Digit;k>0;k--) // 打印出数组中的内容
System.out.print(Data[k]);
System.out.println("");
}
}
}


likgui 2009-01-03
  • 打赏
  • 举报
回复
路过,看看
software51 2009-01-02
  • 打赏
  • 举报
回复
mark
kao331431214 2009-01-02
  • 打赏
  • 举报
回复
学习了
caironghuicxz 2009-01-02
  • 打赏
  • 举报
回复
2楼跟3楼的说的很清楚了啊。
应该能解决了。
cyber_bss 2009-01-01
  • 打赏
  • 举报
回复
同楼上
①超大数用java.math.BigInteger

import java.math.BigInteger;

public class BigIntegerSample {

public static final BigInteger a = new BigInteger("1234567890123");
public static final BigInteger b = new BigInteger("200000000000000000000");

public static void main(String[] args) {
BigInteger result = a.multiply(b);
System.out.println(result.toString());
}
}
输出结果:123456789012300000000000000000000

②带数的用java.math.Decimal

import java.math.BigDecimal;;

public class BigIntegerSample {

public static final BigDecimal a = new BigDecimal("1234567890123.1");
public static final BigDecimal b = new BigDecimal("200000000000000000000.2");

public static void main(String[] args) {
BigDecimal result = a.multiply(b);
System.out.println(result.toString());
}
}

输出结果:246913578024620000000246913578024.62
nicholasmars 2009-01-01
  • 打赏
  • 举报
回复
呵呵,看来我来迟了
加载更多回复(5)

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧