Poj OpenJudge 百练 2389 Bull Math

时间:2021-11-02 00:44:11

1.Link:

http://poj.org/problem?id=2389

http://bailian.openjudge.cn/practice/2389/

2.Content:

Bull Math
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13067   Accepted: 6736

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

3.Method:

直接套大数相乘模板

http://www.cnblogs.com/mobileliker/p/3516920.html

4.Code:

 #include <iostream>
#include <string>
#include <vector>
#include <algorithm> using namespace std; string mul(string str1,string str2)
{
vector<int> v_res(str1.size()+str2.size(),);
string::size_type i,j;
vector<int>::size_type k,p; reverse(str1.begin(),str1.end());
reverse(str2.begin(),str2.end());
for(i = ; i != str1.size(); ++i)
{
for(j = ; j != str2.size(); ++j)
{
v_res[i+j] += (str1[i]-'') * (str2[j] - '');
}
}
for(k = ; k != v_res.size() - ; ++k)
{
v_res[k+] += v_res[k] / ;
v_res[k] = v_res[k] % ;
} for(p = v_res.size() - ; p != -; --p)
{
if(v_res[p] != ) break;
}
if(p == -) p = ; string s_res(p+,'');
for(k = p; k != -; --k) s_res[p-k] = char(v_res[k] + ''); return s_res; } int main()
{
//freopen("D://input.txt","r",stdin); string str1,str2;
cin >> str1 >> str2; cout << mul(str1,str2) << endl; return ;
}

5.Reference: