1.
package problemSetVolumes.volume003;
import java.math.BigInteger;
import java.util.Scanner;
/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS10
* Problem: 338 - Long Multiplication
* Link: http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=274
*
* @author Siegfried Ippisch
* @version 1.0
*
* Method : -
* Status : Accepted
* Runtime: 2.620
*/
public class LongMultiplication {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(true){
long x = in.nextLong();
if(!in.hasNext()) break;
long y = in.nextLong();
longMultiplicaton(x,y);
System.out.println();
}
in.close();
}
private static void longMultiplicaton(long x, long y) {
int nonZero = 0;
// calculate values
int[] y_ = intArray(y);
int lengthY = y_.length;
long[] res = new long[lengthY];
BigInteger sum = BigInteger.ZERO;
BigInteger mul = BigInteger.ONE;
for(int i=lengthY-1; i>=0; i--){
res[i] = x * y_[i];
if(res[i] != 0)
nonZero++;
sum = sum.add(BigInteger.valueOf(res[i]).multiply(mul));
mul = mul.multiply(BigInteger.TEN);
}
// lenghts
int lengthX = (""+x).length();
int lengthSum = (""+sum).length();
int lengthXY = Math.max(lengthX, lengthY);
int length = Math.max(lengthXY, lengthSum);
// x, y, and the first line
System.out.println(setLength(""+x,length));
System.out.println(setLength(""+y,length));
System.out.println(setLength(line(lengthXY),length));
if(nonZero <= 1){
System.out.println(setLength(""+sum,length));
return;
}
// the next lines
for(int i=lengthY-1, l=0; i>=0; i--,l++)
if(res[i] != 0)
System.out.println(setLength(""+res[i],length-l));
// y has only one digit
if(res.length == 1)
return;
// seccond line and result
System.out.println(line(length));
System.out.println(setLength(""+sum,length));
}
private static int[] intArray(long l) {
char[] charArray = (l+"").toCharArray();
int[] intArray = new int[charArray.length];
for(int i=0; i<charArray.length; i++)
intArray[i] = charArray[i]-'0';
return intArray;
}
private static String setLength(String str, int length){
while(str.length() < length)
str = " " + str;
return str;
}
private static String line(int length){
String str = "";
for(int i=0; i<length; i++)
str += '-';
return str;
}
}