POJ 2389 Bull Math

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13500   Accepted: 6968

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

高精度乘法。。CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
#define REP(i, s, n) for(int i = s; i <= n; i ++)
#define REP_(i, s, n) for(int i = n; i >= s; i --)
#define MAX_N 2000

using namespace std;

char a[MAX_N], b[MAX_N];
int la, lb, lc;
int int_a[MAX_N], int_b[MAX_N], int_c[MAX_N];

int main(){
    scanf("%s%s", a + 1, b + 1);
    la = strlen(a + 1); lb = strlen(b + 1);
    memset(int_a, 0, sizeof(int_a)); memset(int_b, 0, sizeof(int_b));
    memset(int_c, 0, sizeof(int_c));
    REP(i, 1, la) int_a[la - i + 1] = a[i] - ‘0‘;
    REP(i, 1, lb) int_b[lb - i + 1] = b[i] - ‘0‘;
    REP(i, 1, la){
        int x = 0;
        REP(j, 1, lb){
            int_c[i + j - 1] = int_a[i] * int_b[j] + x + int_c[i + j - 1];
            x = int_c[i + j - 1] / 10; int_c[i + j - 1] %= 10;
        }
        int_c[i + lb] = x;
    }
    lc = la + lb;
    while(int_c[lc] == 0 && lc > 1) lc--;
    REP_(i, 1, lc) printf("%d", int_c[i]);
    return 0;
}
 
时间: 2024-10-28 06:05:45

POJ 2389 Bull Math的相关文章

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

POJ 2389 Bull Math(大数相乘)

Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13920   Accepted: 7192 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

poj 2389.Bull Math 解题报告

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

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. The

bzoj1754[Usaco2005 qua]Bull Math*

bzoj1754[Usaco2005 qua]Bull Math 题意: 求两个正整数的积,每个数≤40位. 题解: 为什么C++不能支持高精度呢…… 代码: 1 a=int(raw_input()) 2 b=int(raw_input()) 3 print a*b 20160831

poj 1684 Lazy Math Instructor(字符串)

题目链接:http://poj.org/problem?id=1686 思路分析:该问题为表达式求值问题,对于字母使用浮点数替换即可,因为输入中的数字只能是单个digit. 代码如下: #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <string> using namespace

BZOJ 1754: [Usaco2005 qua]Bull Math

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 tw

POJ 1686 Lazy Math Instructor 栈的应用

Description A math instructor is too lazy to grade a question in the exam papers in which students are supposed to produce a complicated formula for the question asked. Students may write correct answers in different forms which makes grading very ha

【BZOJ】1754: [Usaco2005 qua]Bull Math

[算法]高精度乘法 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn=100; char s1[maxn],s2[maxn]; int a[maxn],b[maxn],c[maxn],lena,lenb,lenc; int main(){ scanf("%s%s",s1,s2); lena=strlen(s1);lenb