乘以两个200位数字 [英] Multiply two 200 digit number

查看:60
本文介绍了乘以两个200位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用java编写一个程序,乘以两个200位的数字。







TCS访谈问题..plz帮助

Write a program in java to multiply two 200 digits' number.



TCS Interview Question..plz help

推荐答案

想一想:用铅笔和纸怎么做?



长乘法:

1234 * 5678 = 1000 * 5678 + 200 * 5678 + 30 * 5678 + 4 * 5678.



4 * 5678 = 5000 * 4 + 600 * 4 + 70 * 4 + 8 * 4



...
Think: How would you do it with a pencil and paper?

Long multiplication:
1234*5678 = 1000 * 5678 + 200 * 5678 + 30 * 5678 + 4 * 5678.

4 * 5678 = 5000 * 4 + 600 * 4 + 70 * 4 + 8 * 4

...


因为它听起来像家庭作业,可能你不能使用任何大整数库。因此从两个200字节因子阵列开始,最后得到400字节的产品阵列。

你应该使用在学校学到的基本规则。
Since it sounds like homework, possibly you can't use any big integer library. hence start with the two 200-bytes-factor-arrays and end up with the 400-bytes product-array.
You should use the basic rules learnt at school.


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication5;
 
/**
*
* @author Anil Gola
*/
import java.util.*;
public class JavaApplication5 {
 
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner scan = new Scanner(System.in);
System.out.println("enter first no");
String a=scan.next();
System.out.println("enter second no");
String b=scan.next();
JavaApplication5 mult=new JavaApplication5();
int [] f = convert(a);

int [] s = convert(b);
String [] result = mult.multiply(f, s);
String [] mresult = arrange(result);
String sum="0";
for(int k=0;k<mresult.length;k++)>
{
sum = add(sum,mresult[k]);
}
System.out.println(sum);
}
public static String add(String a1, String b1)
{
int [] a = convert(a1);
int [] b = convert(b1);
int l = a.length-1;
int m = b.length-1;
int sum =0;
int carry = 0;
int rem = 0;
String temp = "";
if(a.length>b.length)
{
while(m>=0)
{
sum = a[l] + b[m] + carry;
carry = sum/10;
rem = sum%10;
temp = rem + temp;
m--;
l--;
}
while(l>=0)
{
sum = a[l] + carry;
carry = sum/10;
rem = sum%10;
temp = rem + temp;
l--;
}
if(carry>0)
{
temp = carry + temp;
}
}
else
{
while(l>=0)
{
sum = a[l] + b[m] + carry;
carry = sum/10;
rem = sum%10;
temp = rem + temp;
m--;
l--;
}
while(m>=0)
{
sum = b[m] + carry;
carry = sum/10;
rem = sum%10;
temp = rem + temp;
m--;
}
if(carry>0)
{
temp = carry + temp;
}
}
return temp;

}


public static int [] convert(String a)
{
int [] temp = new int[a.length()];
for(int i=0;i<a.length();i++)>
{
temp[i] = Character.digit(a.charAt(i), 10);

}

return temp;
}
public static String [] arrange(String [] result)
{
for(int i=0;i<result.length;i++)>
{
int j=0;
while(j<i)>
{
result[i] = result[i] + "0";
j++;
}
}
return result;
}

public String[] multiply(int [] a,int [] b)
{
String [] temp = new String[b.length];
for(int i=b.length-1;i>=0;i--)
{

int carry=0;
int result=0;
int rem=0;
temp[b.length-i-1]="";
for(int j=a.length-1;j>=0;j--)
{
result = a[j]*b[i] + carry;
carry = result/10;
rem = result%10;
temp[b.length - i-1]= rem + temp[b.length - i-1];
}
if(carry>0)
{
temp[b.length -i-1] = carry + temp[b.length -i -1];
}
}
return temp;
}
}


这篇关于乘以两个200位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆