如题。
解决方案 »
- 多态俺懂了,强制类型转换又晕了,大侠指点迷经啊~~~~~
- 排序问题
- 跪求高手....这是第3天了.....
- 求助 连接oracle成功之后如何操作?
- 请教java输入输出程序
- 如何利用f1j包将excel文件(.xls)转换成可编辑的html文件?
- 谁有jakarta common-pool?我现在要用,但是apache的网站上不能下载
- 高手指点指点
- 需要查找Java文件注释代码行的正则表达式
- java.sql.Blob为什么不可以setBinaryStream的,只可以get,如果这样的话,在以前的jdbc版本中是怎样用BLOB数据类型来对表进行操作的,怎
- Myeclipse打包问题
- 求一生成阵列的简便代码
直接搞:int a=123456789;
int b=987654321;
System.out.println((long)a*b);
BigInteger b1 = new BigInteger("123456789");
BigInteger b2 = new BigInteger("987654321");
System.out.println(b1.multiply(b2).toString());
2.利用大数组或string都行,下面给出一个string的实现
ac poj 1001的片断 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;
}
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
}}
他的结果是:121932631112635269
Long最大值:9223372036854775807
没错,是可以的。 只有 2G*2G*2 以上的结果才会溢出。要是我出题,又想考算法的话,我就会附加一句(不能使用long数据类型)
要是我出题,是想考你们对数字范围的概念的话,我就像帖子标题这样讲。拜托,1234567890 还不到2G哩,987654321*987654321都不会溢出哦。我看直接整才是考官想要的吧
{
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);
}
} }
//用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();
}