62,615
社区成员
发帖
与我相关
我的任务
分享
import java.math.BigInteger;
/**
*
* @author wubaochuan
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
BigInteger a = new BigInteger("123456789");
BigInteger b = new BigInteger("987654321");
System.out.println(a.multiply(b));
// TODO code application logic here
}
}
BigInteger b1 = new BigInteger("123456789");
BigInteger b2 = new BigInteger("987654321");
System.out.println(b1.multiply(b2).toString());
public String add(String s1,String s2) throws Exception{
if(s1 == null) throw new Exception("输入错误");
if(s2 == null) throw new Exception("输入错误");
String regex = "[\\d]+$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s1);
if(!matcher.find()) throw new Exception("输入错误");
matcher = pattern.matcher(s2);
if(!matcher.find()) throw new Exception("输入错误");
String result1 = "";
int minLen =0;
int maxLen = 0;
String longString = "";
String shortString = "";
if(s1.length() > s2.length()) {
maxLen = s1.length();
minLen = s2.length();
longString = s1;
shortString = s2;
}else {
maxLen = s2.length();
minLen = s1.length();
longString = s2;
shortString = s1;
}
int carry = 0;
StringBuffer result = new StringBuffer();
int tempResult = 0;
for(int i = maxLen-1,j=minLen-1;j>-1;i--,j--) {
tempResult = Integer.parseInt(longString.substring(i, i+1)) + Integer.parseInt(shortString.substring(j, j+1)) + carry;
result.append(tempResult%10);
carry = (int)(tempResult/10);
}
for(int i=maxLen-minLen-1;i>-1;i--) {
tempResult = Integer.parseInt(longString.substring(i, i+1)) + carry;
result.append(tempResult%10);
carry = (int)(tempResult/10);
}
if(carry>0) {
result.append(carry);
}
return result.reverse().toString();
}
private String multi(String s1,String s2) throws Exception {
if(s1 == null) throw new Exception("输入错误");
if(s2 == null) throw new Exception("输入错误");
if(s2.length()!=1) throw new Exception("输入错误");
String regex = "[\\d]+$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s1);
if(!matcher.find()) throw new Exception("输入错误");
matcher = pattern.matcher(s2);
if(!matcher.find()) throw new Exception("输入错误");
String result1 = "";
int tempResult = 0;
int carry = 0;
StringBuffer result = new StringBuffer();
int multi = Integer.parseInt(s2);
for(int i=s1.length()-1;i>-1;i--) {
tempResult = Integer.parseInt(s1.substring(i, i+1)) * multi + carry;
result.append(tempResult%10);
carry = (int)(tempResult/10);
}
if(carry>0) {
result.append(carry);
}
return result.reverse().toString();
}
public String multiple(String s1,String s2) throws Exception {
if(s1 == null) throw new Exception("输入错误");
if(s2 == null) throw new Exception("输入错误");
String regex = "[\\d]+$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s1);
if(!matcher.find()) throw new Exception("输入错误");
matcher = pattern.matcher(s2);
if(!matcher.find()) throw new Exception("输入错误");
String result = "0";
String tempResult = "";
StringBuffer tempBuffer;
for(int i=0;i<s2.length();i++) {
tempResult = this.multi(s1, s2.substring(i, i+1));
tempBuffer = new StringBuffer(tempResult);
for(int j=i;j<s2.length()-1;j++) {
tempBuffer.append("0");
}
result = this.add(tempBuffer.toString(), result);
}
return result;
}
int a=123456789;
int b=987654321;
System.out.println((long)a*b);
//用String好了
public static String cm(String o, String t) {
StringBuffer o1 = new StringBuffer(o).reverse();
StringBuffer t1 = new StringBuffer(t).reverse();
StringBuilder jg = new StringBuilder("0");
int sum = 0;//当前位
for (int i = 0, j; i < o1.length(); i++) {
for (j = 0; j < t1.length(); j++) {
sum += (o1.charAt(i) - '0') * (t1.charAt(j) - '0');
if (jg.length() - 1 < i + j) {
jg.append('0');
}
sum += jg.charAt(i + j) - '0';
jg.replace(i + j, i + j + 1, "" + (char) (sum % 10 + '0'));
sum /= 10;
}
while (sum > 0) {
if (jg.length() - 1 < i + j) {
jg.append('0');
}
sum += jg.charAt(i + j) - '0';
jg.replace(i + j, i + j + 1, "" + (char) (sum % 10 + '0'));
sum /= 10;
j++;
}
}
return jg.reverse().toString();
}
class big
{
public static void main(String[] args)
{
int[] n1={1,2,3,4,5,6,7,8,9,1,2,3,4,5,6};
int[] n2={9,8,7,6,5,4,3,2,1,3,4,5,6,7,8,9,9};
int[] result=multiply(n1,n2);
for(int i = 0; i < result.length; i++)
{
System.out.print(result[i]);
}
}
private static int[] multiply(int[] numbers1, int[] numbers2)
{
int[] result = new int[numbers1.length + numbers2.length];
for (int i = 0; i < numbers1.length; i++) {
for (int j = 0; j < numbers2.length; j++) {
int tenth = i + j;
int cellResult = numbers1[i] * numbers2[j];
put(result, tenth + 1, cellResult % 10);
put(result, tenth, cellResult / 10);
}
}
return result;
}
private static void put(int[] result, int index, int number) {
result[index] += number;
carryFrom(result, index);
}
private static void carryFrom(int[] result, int index) {
if (index < 0) {
return;
}
if (result[index] >= 10) {
result[index - 1]++;
result[index] = (int)(result[index] - 10);
carryFrom(result, index - 1);
}
}
}