BigIntegerMultiplyWXJ
来源:互联网 发布:梦想世界辅助软件 编辑:程序博客网 时间:2024/05/18 02:54
package hwtest.total;
import java.util.ArrayList;
import java.util.List;
public class BigIntegerMultiplyWXJ {
public static List<Integer> longToList(int d) {
List<Integer> list = new ArrayList();
while (d > 0) {
list.add((d % 10));
d /= 10;
}
// System.out.println(list);
return list;
}
public static String listToString(List<Integer> list) {
StringBuffer sb = new StringBuffer();
int p = list.size() - 1;
while (p >= 0)
sb.append(list.get(p--));
return sb.toString();
}
public static List<Integer> bigIntegerMultiply(List<Integer> a, List<Integer> b) {
List<Integer> list = new ArrayList(a.size() + b.size());
int t;
for (int i = 0; i < a.size() + b.size(); i++)
list.add(0);
// System.out.println("list:"+list);
for (int i = 0; i < a.size(); i++)
for (int j = 0; j < b.size(); j++) {
list.set(i + j, list.get(i + j) + a.get(i) * b.get(j));
}
for (int i = 0; i < list.size() - 1; i++) {
t = list.get(i);
list.set(i, t % 10);
list.set(i + 1, list.get(i + 1) + t / 10);
}
while (list.size() > 0 && list.get(list.size() - 1) == 0)
list.remove(list.size() - 1);
// System.out.println("list:"+list);
return list;
}
public static String bigIntegerMultiplyN(int n) {
List<Integer> list = new ArrayList();
List<Integer> result = longToList(n);
n--;
List<Integer> listN = new ArrayList();
while (n > 1) {
listN = longToList(n);
result = bigIntegerMultiply(result, listN);
n--;
}
return listToString(result);
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.out.println("bigIntegerMultiplyN:" + bigIntegerMultiplyN(1000));
}
}
import java.util.ArrayList;
import java.util.List;
public class BigIntegerMultiplyWXJ {
public static List<Integer> longToList(int d) {
List<Integer> list = new ArrayList();
while (d > 0) {
list.add((d % 10));
d /= 10;
}
// System.out.println(list);
return list;
}
public static String listToString(List<Integer> list) {
StringBuffer sb = new StringBuffer();
int p = list.size() - 1;
while (p >= 0)
sb.append(list.get(p--));
return sb.toString();
}
public static List<Integer> bigIntegerMultiply(List<Integer> a, List<Integer> b) {
List<Integer> list = new ArrayList(a.size() + b.size());
int t;
for (int i = 0; i < a.size() + b.size(); i++)
list.add(0);
// System.out.println("list:"+list);
for (int i = 0; i < a.size(); i++)
for (int j = 0; j < b.size(); j++) {
list.set(i + j, list.get(i + j) + a.get(i) * b.get(j));
}
for (int i = 0; i < list.size() - 1; i++) {
t = list.get(i);
list.set(i, t % 10);
list.set(i + 1, list.get(i + 1) + t / 10);
}
while (list.size() > 0 && list.get(list.size() - 1) == 0)
list.remove(list.size() - 1);
// System.out.println("list:"+list);
return list;
}
public static String bigIntegerMultiplyN(int n) {
List<Integer> list = new ArrayList();
List<Integer> result = longToList(n);
n--;
List<Integer> listN = new ArrayList();
while (n > 1) {
listN = longToList(n);
result = bigIntegerMultiply(result, listN);
n--;
}
return listToString(result);
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.out.println("bigIntegerMultiplyN:" + bigIntegerMultiplyN(1000));
}
}
阅读全文
0 0
- BigIntegerMultiplyWXJ
- 2017.08.10工作日记
- #if,#ifdef和#if defined对于条件编译#if,#ifdef和#if defined这些,深究起来还是有些意思 #if要求是一个表达式,为真则执行以下代码, #ifdef则只要求是
- 关于打印图形
- 【如何用XAMPP搭建Wordpress建站环境】
- 正则表达式和python结合
- BigIntegerMultiplyWXJ
- kwk
- 面试经历
- 细说 AppbarLayout,如何理解可折叠 Toolbar 的定制
- java基础之面向对象(1)
- Java程序入门(二)
- [第六季]10.对象、数组及表格处理
- 关于Map和HashMap的思考
- http错误码