Multiply Strings 大数相乘 java

先贴上代码

 1 public String multiply(String num1, String num2) {
 2         String str = "";
 3         StringBuffer sb = new StringBuffer(num1);
 4         num1 = sb.reverse().toString();
 5         sb = new StringBuffer(num2);
 6         num2 = sb.reverse().toString();
 7         int[] res = new int[num1.length() + num2.length()];
 8         for (int i = 0; i < num1.length(); i++) {
 9             for (int j = 0; j < num2.length(); j++) {
10                 res[i + j] += (num1.charAt(i) - ‘0‘) * (num2.charAt(j) - ‘0‘);
11             }
12         }
13         int k = 0, contribute = 0;
14         while (k < res.length || contribute > 0) {
15             int tmp = k<res.length?res[k]:0;
16             tmp = contribute+tmp;
17             contribute = tmp/10;
18             str += tmp%10;
19             k++;
20         }
21         sb = new StringBuffer(str);
22         str = sb.reverse().toString();
23         int i = 0;
24         while(i<str.length()&&str.charAt(i)==‘0‘)
25             i++;
26         if(i<str.length()){
27             str = str.substring(i);
28         }else{
29             str = "0";
30         }
31         return str;
32     }

思路如下:

模拟竖式相乘

1、转换并反转,字序反转;
2、逐位相乘,结果存放在res[i+j]中;
3、处理进位;
4、转换并反转,将计算结果转换为字符串并反转。
5、消除多余的0;

两数相乘,结果的长度一定不大于乘数和被乘数的长度之和。

上述也可以用直接用java的BigInteger类实现

public static void main(String[] args) {
        String a = "98989898989898956898", b = "989892551548781251323265615150";
        BigInteger aa = new BigInteger(a);
        BigInteger bb = new BigInteger(b);
        System.out.println(aa.multiply(bb));

    }

时间: 2024-11-07 05:31:26

Multiply Strings 大数相乘 java的相关文章

leetcode 43 Multiply Strings 大数相乘

感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(num1.charAt(0) == '0' || num2.charAt(0) == '0'){ return "0"; } int len1 = num1.length(); int len2 = num2.length(); int len = len1+len2; int[] arr =

[LeetCode] Multiply Strings 字符串相乘

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 这道题让我们求两个字符串数字的相乘,输入的两个数和返回的数都是以字符串格式储存的,这样做的原因可能是这样可以计算超大数相乘,可以不受int或long的数值范围的约束,那么我们该如何来计算

43. Multiply Strings 字符串相乘

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2. Note: The length of both num1 and num2 is < 110. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading z

Multiply Strings 字符串相乘

http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/48055617 class Solution { public: string multiply(string num1, string num2) { int len1 = num1.length(); int len2 = num2.length(); vector<int> res(len1+len2,

LeetCode 43. 字符串相乘(Multiply Strings)

43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. LeetCode43. Multiply Strings中等 示例 1: 输入: num1 = "2", num2 = "3" 输出: "6" 示例?2: 输入: num1 = "123", num2 = "456&q

Multiply Strings leetcode java

题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 题解: 题意就是给你两个字符串型的数字,给这两个数字做乘法. 如果直接转换成Integer做乘法就会溢出. 所以要一步一步来. 下面讲解引用自(http://leetcodeno

Java大数相乘

import java.io.*; import java.util.*; import java.text.*; import java.math.*; public class Main { public static void main(String []args) { Scanner cin = new Scanner(new BufferedInputStream(System.in)); while(cin.hasNext()) { BigInteger a=cin.nextBigI

java版大数相乘

在搞ACM的时候遇到大数相乘的问题,在网上找了一下,看到了一个c++版本的 http://blog.csdn.net/jianzhibeihang/article/details/4948267 用java搞了一个版本 这里说一下思路 将数字已字符串形式接收,转换成int[]整型数组,然后num1[],num2[]依次相乘,结果保存到result[]中 其他注意的在注释中有说明 1 package com.gxf.test; 2 3 import java.util.Scanner; 4 5 p

Multiply Strings leetcode

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. Subscribe to see which companies asked this question ACM竞赛常考的大数相乘算法,利用字符串来表示大数字进行计算. string m