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

USACO 2004 November

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:

时间: 2024-08-13 20:11:32

Poj OpenJudge 百练 2389 Bull Math的相关文章

Poj OpenJudge 百练 1860 Currency Exchang

1.Link: http://poj.org/problem?id=1860 http://bailian.openjudge.cn/practice/1860 2.Content: Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20706   Accepted: 7428 Description Several currency exchange points are working

Poj OpenJudge 百练 2602 Superlong sums

1.Link: http://poj.org/problem?id=2602 http://bailian.openjudge.cn/practice/2602/ 2.Content: Superlong sums Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22337   Accepted: 6577 Description The creators of a new programming language D++

Poj OpenJudge 百练 1062 昂贵的聘礼

1.Link: http://poj.org/problem?id=1062 http://bailian.openjudge.cn/practice/1062/ 2.Content: 昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37631   Accepted: 10872 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金

Poj OpenJudge 百练 1573 Robot Motion

1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10856   Accepted: 5260 Description A robot has been programmed to follow the instru

Poj OpenJudge 百练 2632 Crashing Robots

1.Link: http://poj.org/problem?id=2632 http://bailian.openjudge.cn/practice/2632/ 2.Content: Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7912   Accepted: 3441 Description In a modernized warehouse, robots are used to

Poj OpenJudge 百练 Bailian 1008 Maya Calendar

1.Link: http://poj.org/problem?id=1008 http://bailian.openjudge.cn/practice/1008/ 2.content: Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 66971   Accepted: 20644 Description During his last sabbatical, professor M. A. Ya

[OpenJudge] 百练2754 八皇后

八皇后 Description 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某个满足要求的8皇后的摆放方法,定义一个皇后串a与之对应,即a=b1b2...b8,其中bi为相应摆法中第i行皇后所处的列数.已经知道8皇后问题一共有92组解(即92个不同的皇后串).给出一个数b,要求输出第b个串.串的比较是这样的:皇后串x置于皇后串y之前,当且仅当将x视为整数时比y小. I

poj 2389.Bull Math 解题报告

题目链接:http://poj.org/problem?id=2389 题目意思:就是大整数乘法. 题目中说每个整数不超过 40 位,是错的!!!要开大点,这里我开到100. 其实大整数乘法还是第一次写 = =.......大整数加法写得比较多.百练也有一条是大整数乘法,链接如下:http://bailian.openjudge.cn/practice/2980/ 一步一步模拟即可,代码就是按这个来写的. 以 835 * 49 为例(亲爱的读者,允许我截图吧) 简直就是神奇呀----- 1 #i

POJ 2389 Bull Math(大数乘法,还是Java好)

Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14252   Accepted: 7350 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. F