面试算法--大数据相乘(正数)

一丶数组实现

import java.util.Scanner;

public class WDBigNumMultip {
    static int N = 1000;
    static int a[] = new int[N];
    static int b[] = new int[N];

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s1 = scanner.nextLine().trim();
        String s2 = scanner.nextLine().trim();
        int len1 = s1.length();
        int len2 = s2.length();
        getDigit(s1, a);
        getDigit(s2, b);
        doOutput(doMultiply(a, len1, b, len2));

    }

    public static void doOutput(int[] arr) {
        boolean findFirstNum = false;
        for (int i = arr.length - 1; i >= 0; i--) {
            if (!findFirstNum && arr[i] == 0) {
                continue;
            }
            findFirstNum = true;
            System.out.print(arr[i]);
        }
        System.out.println("");
    }

    public static int[] doMultiply(int[] a, int len1, int[] b, int len2) {
        int res[] = new int[2 * N + 1];
        for (int i = 0; i < len1; i++) {
            for (int j = 0; j < len2; j++) {
                int temp = res[j + i] + a[i] * b[j];
                int n = temp % 10;
                int m = temp / 10;
                res[i + j] = n;
                res[i + j + 1] += m;
            }
        }
        return res;
    }

    public static void getDigit(String s, int[] a) {
        int len = s.length();
        for (int i = 0; i < len; i++) {
            a[len - 1 - i] = s.charAt(i) - '0';
        }
    }
}

二丶字符串实现

public class WDBigCount {

    public static void main(String[] args) {
        String str1 = "2";
        String str2 = "2";

        int len1 = str1.length();
        int len2 = str2.length();

        char[] s1 = str1.toCharArray();
        char[] s2 = str2.toCharArray();

        // 高低位对调
        covertdata(s1, len1);
        covertdata(s2, len2);
        System.out.println("乘数:" + str1);
        System.out.println("乘数:" + str2);
        multiply(s1, len1, s2, len2);

    }

    public static void covertdata(char data[], int len) {
        //高低位对调
        for (int i = 0; i < len / 2; i++) {
            data[i] += data[len - 1 - i];
            data[len - 1 - i] = (char) (data[i] - data[len - 1 - i]);
            data[i] = (char) (data[i] - data[len - 1 - i]);
        }
    }

    public static void multiply(char a[], int alen, char b[], int blen) {
        // 两数乘积位数不会超过乘数位数和+ 3位
        int csize = alen + blen + 3;
        // 开辟乘积数组
        int[] c = new int[csize];
        //      // 乘积数组填充0
        //      for (int ii = 0; ii < csize; ii++) {
        //          c[ii] = 0;
        //      }
        // 对齐逐位相乘
        for (int j = 0; j < blen; j++) {
            for (int i = 0; i < alen; i++) {
                c[i + j] += Integer.parseInt(String.valueOf(a[i])) * Integer.parseInt(String.valueOf(b[j]));
            }
        }
        int m = 0;
        // 进位处理
        for (m = 0; m < csize; m++) {
            int carry = c[m] / 10;
            c[m] = c[m] % 10;
            if (carry > 0)
                c[m + 1] += carry;
        }
        // 找到最高位
        for (m = csize - 1; m >= 0;) {
            if (c[m] > 0)
                break;
            m--;
        }
        // 由最高位开始打印乘积
        System.out.print("乘积:");
        for (int n = 0; n <= m; n++) {
            System.out.print(c[m - n]);
        }
        System.out.println("");
    }
}

你可能感兴趣的:(面试算法--大数据相乘(正数))