POJ 2389

Bull Math

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16175   Accepted: 8268

Description

Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls' answers. Read in two positive integers (no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros). 

FJ asks that you do this yourself; don't use a special library function for the multiplication.

Input

* Lines 1..2: Each line contains a single decimal number.

Output

* Line 1: The exact product of the two input lines

Sample Input

11111111111111
1111111111

Sample Output

12345679011110987654321

Source

USACO 2004 November

代码:

 
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int sum[2001];
int s1[50] , s2[50];
string str1 , str2;
void multiply()
{
    int i , j , k;
    int l1 , l2 , len;
    memset(sum,0,sizeof(sum));
    memset(s1,0,sizeof(s1));
    memset(s2,0,sizeof(s2));
    l1 = str1.size();
    l2 = str2.size();
    for(i = l1-1 , k = 1;i >= 0;i-- , k++)
        s1[k] = str1[i]-48;
    for(i = l2-1 , k = 1;i >= 0;i-- , k++)
        s2[k] = str2[i]-48;
    for(i = 1;i <= l1;i++ )
    {
        for(j = 1;j <= l2;j++)
        {
            sum[i+j-1] += s1[i]*s2[j];
            sum[i+j] += sum[i+j-1]/10;
            sum[i+j-1] %= 10;
        }
    }
    len = 2000;
    while(len--)
    {
        if(sum[len] != 0)
            break;
    }
    for(;len >= 1;len--)
        cout<<sum[len];
    cout<<endl;
}
int main()
{
    while(cin>>str1>>str2)
    {
        multiply();  
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40421671/article/details/82822226
POJ