UVA - 10106 Product

来源:互联网 发布:金融数据建模招聘 编辑:程序博客网 时间:2024/05/16 01:46

UVA - 10106 Product

题目大意:高精乘法
解题思路:用两个数组存 其中一个数组每个元素乘以另一个 数组的每个元素 然后就行了

/*************************************************************************    > File Name: UVA-10106.cpp    > Author: Robin    > Mail: 499549060@qq.com     > Created Time: 2016年07月19日 星期二 14时46分37秒 ************************************************************************/#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>using namespace std;int main() {    char x1[100000],x2[100000];    int a1[100000];    int a2[100000];    int a3[100000];    while (scanf("%s%s", x1, x2) != EOF) {        memset(a1, 0, sizeof(a1));        memset(a2, 0, sizeof(a2));        memset(a3, 0, sizeof(a3));        int len1 = strlen(x1);        int len2 = strlen(x2);        int jj = 0;        for (int i = len1 - 1; i >= 0; i--, jj++)             a1[jj] = x1[i] - '0';        jj = 0;        for (int i = len2 - 1; i >= 0; i--, jj++)            a2[jj] = x2[i] - '0';        for (int i = 0; i < len1; i++) {            int s = 0;            for (int j = 0; j < len2; j++) {                s = a3[i+j]+a1[i]*a2[j];                a3[j+i] = s%10;                a3[i+j+1] += s/10;            }        }        int f;        if (a3[len1 + len2 - 1] > 0) {            f =  len1 + len2 - 1;        }        else             f = len1 + len2 - 2;        for (int i = f; i >= 0; i--) {            printf("%d", a3[i]);            if (a3[i] == 0 && i == f)                break;        }        printf("\n");    }}
0 0
原创粉丝点击