开发者

string multiplication

I a开发者_如何学编程m trying to multiply two strings, but I am getting the wrong answer. Any help will be appreciated:

public class stringmultiplication {
    public static void main(String[] args) {
        String s1 = "10";
        String s2 = "20";
        int num = 0;
        for(int i = (s1.toCharArray().length); i > 0; i--)
            for(int j = (s2.toCharArray().length); j > 0; j--)
                num = (num * 10) + ((s1.toCharArray()[i - 1] - '0') * (s2.toCharArray()[j - 1] - '0'));
        System.out.println(num);
    }
}


public static void main(String[] args) {
        String number1 = "108";
        String number2 = "84";

        char[] n1 = number1.toCharArray();
        char[] n2 = number2.toCharArray();

        int result = 0;

        for (int i = 0; i < n1.length; i++) {
            for (int j = 0; j < n2.length; j++) {
                result += (n1[i] - '0') * (n2[j] - '0')
                        * (int) Math.pow(10, n1.length + n2.length - (i + j + 2));
            }
        }
        System.out.println(result);
    }

This one should be correct implementation without using integers.


You're multiplying the numbers digit-wise, and you're not handling the powers of 10 correctly.

You need to first parse the strings into integers. You're on the right track here. You can simplify the loop indices, and you only have to call toCharArray once. E.g.:

After parsing, you can multiply the integers.

EDIT: If that's not allowed, you need to implement an algorithm like this one, which is a bit more complicated.

One approach is to make an (n + 1) x (m + n) array (strictly an array of arrays), where m and n are the number of digits in each. It will be initialized to 0, and you can use this as an area to put the rows of the immediate and final results. These are then summed with carry. This is obviously a näive algorithm.

E.g. for the example above:

int[][] intermediates = new int[3][4];

This is an upper bound.


Following is the solution which i suggest, what you forgot doing there is keeping the intermediate value.

public class T{  
    public static void main(String[] args) {     

        char[] num1 = "127".toCharArray();     
        char[] num2 = "32".toCharArray();

        int[] intermediate = new int[num1.length];

        for (int i = 0 ; i < num1.length ; i++ )  { 

                for(int j = 0 ; j < num2.length ; j++ ) { 


                  int d1 = num1[num1.length - i - 1]-'0';
                  int d2 = num2[num2.length - j - 1]-'0';


                  intermediate[i] += d1 * d2 * (int) Math.pow(10,j);

                  System.out.printf("  %d X %d = %d\n", d1, d2, intermediate[i]);

                }     

             intermediate[i] *= (int) Math.pow(10,i);

             System.out.println(" intermediate : " + intermediate[i]);
        }     


        int sum = 0;

        for(int i : intermediate) {
            sum += i;
        }

        System.out.println("Sum is = " + sum); 
    }
} 


I found Peter's Algorithm using the pow function to be a bit confusing. Here is essentially the same algorithm. Convert your Strings to char[]'s and then run this.

public static int multiply (char A[], char B[]){
		int totalSum = 0, sum = 0;
		for (int i = 0; i < A.length; i++){
			sum = 0;

			for (int j = 0; j < B.length; j++){
				sum *= 10;
				sum += (A[i] - '0') * (B[j] - '0');
				
			}
			totalSum *=10;
			totalSum += sum;
		}

		return totalSum;
	}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜