首页 > 解决方案 > 存储为字符类型的数组列表的大数的乘法

问题描述

 private ArrayList<Character> product(ArrayList<Character> list1, ArrayList<Character> list2) {
        int size1 = list1.size();
        int size2 = list2.size();
        ArrayList<Character> list3 = new ArrayList<Character>();
        ArrayList<Character> list4 = new ArrayList<Character>();
        for (int index = (size2 -1); index >= 0; index--) {
            int carryOver = 0;
            int charInt1 = (int) ( list2.get(index) -'0');
            int count = index;
            while(count > 0) {
                list3.add('0');
                count--;
            }
            for ( int key = 0; key < size1; key++) {
                int charInt2 = (int) ( list1.get(key) - '0');
                int product = (charInt1 * charInt2) + carryOver;
                char intChar = (char) ( (product % 10) + 48);
                list3.add(intChar);
                carryOver = product / 10;

            }
            if ( carryOver != 0) {
                char carryChar = (char) ( carryOver + 48);
                list3.add(carryChar);
            }

            list4 = sum (list3, list4);
            list3.clear();

        }
        return list4;
    }

这是用于存储在字符类型的 ArrayLists 中的大数的乘法。这段代码不适用于 32 * 32、91 * 11 等情况。方法 sum 运行良好。

标签: javaarraylistmultiplication

解决方案


推荐阅读