Bull Math(高精度)

来源:互联网 发布:中国it人才网 编辑:程序博客网 时间:2024/04/30 07:39

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

111111111111111111111111

Sample Output

12345679011110987654321

解题思路:

普通的高精度乘法题。

AC代码:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 100;int main(){    int num_1[maxn], num_2[maxn], length_1, length_2, result[maxn * 2];    char str_1[maxn], str_2[maxn];    scanf("%s%s", str_1, str_2);    memset(num_1, 0, sizeof(num_1));    memset(num_2, 0, sizeof(num_2));    memset(result,0,sizeof(result));    length_1 = strlen(str_1);    length_2 = strlen(str_2);    for(int i = 0; i < length_1; i++)    {        num_1[i] = str_1[length_1-i-1] - '0';    }    for(int i = 0; i < length_2; i++)    {        num_2[i] = str_2[length_2-i-1] - '0';    }    for(int i = 0; i < length_1; i++)        for(int j = 0; j < length_2; j++)        {            result[i + j] += num_1[i] * num_2[j];        }    for(int i = 0; i < maxn * 2; i++)    {        if(result[i] >= 10)        {            result[i + 1] += result[i]/10;            result[i] %= 10;        }    }    bool isBegin = false;    for(int i = maxn * 2 - 1; i >= 0; i--)    {        if(isBegin)            printf("%d",result[i]);        else if(result[i])        {            printf("%d",result[i]);            isBegin = true;        }    }    if(!isBegin)        printf("0");    return 0;}


0 0
原创粉丝点击