java - 存储为字符类型的数组列表的大数的乘法
问题描述
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 运行良好。
解决方案
推荐阅读
- javascript - 如何使用正则表达式验证 ISO8601 日期?
- python - (Python)获得最大递归错误,在这种情况下我应该尝试另一种方法还是覆盖它?
- php - php在表单外提交变量?
- dynamic - 考试包 - 为 Moodle 设置多项练习测验
- c - 为什么不是正在读取的文件的最后一个数值?
- javascript - 文本不显示在图像上 CSS HTML JS
- dart - 为什么 Dart 充当引用传递?
- reactjs - 如何在 Material UI 中将鼠标悬停在卡片上时放大卡片大小?
- python - Discord.py bot 中断 mp3
- html - 为什么我的网站没有使用 Bootstrap 覆盖整个屏幕?