【华为面试题】123456789*987654321用java实现,谢谢

Fenglee2008 2010-11-15 11:34:30
如题。
...全文
940 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
Fenglee2008 2010-11-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 houjin_cn 的回复:]
要啥大整数:
直接搞:

Java code

int a=123456789;
int b=987654321;
System.out.println((long)a*b);
[/Quote]
==================
这个是要考算法的,原题是不限语言的,直接弄,肯定不行。
phpjspasp 2010-11-15
  • 打赏
  • 举报
回复

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
}

}

yaoweijq 2010-11-15
  • 打赏
  • 举报
回复
1.java.math.biginteger
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;
}
so_so_ 2010-11-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 houjin_cn 的回复:]

要啥大整数:
直接搞:
Java code

int a=123456789;
int b=987654321;
System.out.println((long)a*b);
[/Quote]

...
houjin_cn 2010-11-15
  • 打赏
  • 举报
回复
要啥大整数:
直接搞:

int a=123456789;
int b=987654321;
System.out.println((long)a*b);
lost_guy_in_scut 2010-11-15
  • 打赏
  • 举报
回复
利用大数组既可实现。
asd1985223 2010-11-15
  • 打赏
  • 举报
回复
....很给力
[Quote=引用 2 楼 houjin_cn 的回复:]
要啥大整数:
直接搞:

Java code

int a=123456789;
int b=987654321;
System.out.println((long)a*b);
[/Quote]
bawgiitx 2010-11-15
  • 打赏
  • 举报
回复

//用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();
}
niuge798588567 2010-11-15
  • 打赏
  • 举报
回复
哈哈哈,被他欺骗啦,int很牛的,别小看它啦!!!
oO寒枫Oo 2010-11-15
  • 打赏
  • 举报
回复
12楼那个随便输入整数的位数都可以算出来 输入参数转化一下应该不难的
clariones 2010-11-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 fenglee2008 的回复:]

引用 9 楼 clariones 的回复:
直接整说明你对long的取值范围很敏感。
。。。。。。。。。。。。。。。。。。。。
=======================================
考官后来说了,想要的是算法,考虑用数组实现。还有,华为一般不会考些语言基础题目,而是考察算法思维。
[/Quote]
那估计没啥好选的了,就是考大数相乘的算法了。 输入是 string*string,输出还是string,里面用byte[], 算法还是标准乘法。
小裴同学 2010-11-15
  • 打赏
  • 举报
回复
数组。。用String不行么。还算法这题目出的真纠结。。
oO寒枫Oo 2010-11-15
  • 打赏
  • 举报
回复
估计是要这样的吧 求的是 (123456789123456*98765432134567899)

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);
}
}

}
howo2010 2010-11-15
  • 打赏
  • 举报
回复
复杂。。。。。。。。。。。。。。。。。。加油!
Fenglee2008 2010-11-15
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 clariones 的回复:]
直接整说明你对long的取值范围很敏感。
他的结果是:121932631112635269
Long最大值:9223372036854775807
没错,是可以的。 只有 2G*2G*2 以上的结果才会溢出。

要是我出题,又想考算法的话,我就会附加一句(不能使用long数据类型)
要是我出题,是想考你们对数字范围的概念的话,我就像帖子标题这样讲。

拜托,1234567890 还……
[/Quote]
===================================
考官后来说了,想要的是算法,考虑用数组实现。还有,华为一般不会考些语言基础题目,而是考察算法思维。
clariones 2010-11-15
  • 打赏
  • 举报
回复
直接整说明你对long的取值范围很敏感。
他的结果是:121932631112635269
Long最大值:9223372036854775807
没错,是可以的。 只有 2G*2G*2 以上的结果才会溢出。

要是我出题,又想考算法的话,我就会附加一句(不能使用long数据类型)
要是我出题,是想考你们对数字范围的概念的话,我就像帖子标题这样讲。

拜托,1234567890 还不到2G哩,987654321*987654321都不会溢出哦。

我看直接整才是考官想要的吧
djvfe 2010-11-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 houjin_cn 的回复:]
引用 6 楼 fenglee2008 的回复:
引用 2 楼 houjin_cn 的回复:
这个是要考算法的,原题是不限语言的,直接弄,肯定不行。

那究竟有啥要求, 有啥限制, 说清楚啊, 难道不能用long型, 哪个语言都支持这个吧
[/Quote]
如果不用大数,就自己用字符串或数组模拟实现
houjin_cn 2010-11-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fenglee2008 的回复:]
引用 2 楼 houjin_cn 的回复:
这个是要考算法的,原题是不限语言的,直接弄,肯定不行。
[/Quote]
那究竟有啥要求, 有啥限制, 说清楚啊, 难道不能用long型, 哪个语言都支持这个吧

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧