大数乘法

import java.util.Arrays;

public class BigIntMul
{

    public static void multipy( String num1, String num2 )
    {
        if( num1 == null && num1.trim( ).length( )== 0 )
        {
            return;
        }
        if( num2 == null && num2.trim( ).length( )== 0 )
        {
            return;
        }
        int i, j;
        int len = num1.length( ) + num2.length( );
        int result[] = new int[len];
        Arrays.fill( result, 0 );
        for( i = 0; i < num1.length( ); i++ )
            for( j = 0; j < num2.length( ); j++ )
            {
                char op1 = num1.charAt( i );
                int mul1 = Integer.parseInt( Character.toString( op1 ) );
                char op2 = num2.charAt( j );
                int mul2 = Integer.parseInt( Character.toString( op2 ) );
                result[ i+j+1 ] += mul1 * mul2;
            }
        // handle carry over
        for( i = len-1; i > 0; i-- )
        {
            if( result[i] >= 10 )
            {
                result[i-1] += result[i] /10;
                result[i] %= 10;
            }
        }
        for( i = 0; i < result.length; i++ )
        {
            System.out.print( result[i] + " " );
        }
    }
    /**
     * @param args
     */
    public static void main( String[] args )
    {
        multipy( "98485132987489729739127391723", "639283749375839742897492873289" );
    }

}

Advertisements
This entry was posted in programming and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s