Poj OpenJudge 百练 2389 Bull Math

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:

 1 #include <iostream>

 2 #include <string>

 3 #include <vector>

 4 #include <algorithm>

 5 

 6 using namespace std;

 7 

 8 string mul(string str1,string str2)

 9 {

10     vector<int> v_res(str1.size()+str2.size(),0);

11     string::size_type i,j;

12     vector<int>::size_type k,p;

13 

14     reverse(str1.begin(),str1.end());

15     reverse(str2.begin(),str2.end());

16     for(i = 0; i != str1.size(); ++i)

17     {

18         for(j = 0; j != str2.size(); ++j)

19         {

20             v_res[i+j] += (str1[i]-'0') * (str2[j] - '0');

21         }

22     }

23     for(k = 0; k != v_res.size() - 1; ++k)

24     {

25         v_res[k+1] += v_res[k] / 10;

26         v_res[k] = v_res[k] % 10;

27     }

28 

29     for(p = v_res.size() - 1; p != -1; --p)

30     {

31         if(v_res[p] != 0) break;

32     }

33     if(p == -1) p = 0;

34 

35     string s_res(p+1,'0');

36     for(k = p; k != -1; --k) s_res[p-k] = char(v_res[k] + '0');

37     

38 

39     return s_res;

40 

41 }

42 

43 int main()

44 {

45     //freopen("D://input.txt","r",stdin);

46     

47     string str1,str2;

48     cin >> str1 >> str2;

49 

50     cout << mul(str1,str2) << endl;

51 

52     return 0;

53 }

5.Reference:

你可能感兴趣的:(Math)