大整数乘法CPP

来源:互联网 发布:mysql查询子语句 编辑:程序博客网 时间:2024/06/03 15:03

描述

求两个不超过200位的非负整数的积。

输入

有两行,每行是一个不超过200位的非负整数,没有多余的前导0。

输出

一行,即相乘后的结果。结果里不能有多余的前导0,即如果结果是342,那么就不能输出为0342。

#include<iostream>#include<algorithm>#include<string>#include<string.h>using namespace std;const int maxn = 205;int main(){string str1, str2;int a[maxn];int b[maxn];int len1, len2, i, j;cin >> str1 >> str2;len1 = str1.length();len2 = str2.length();for (i = 0, j = len1 - 1; i < len1; i++, j--)a[i] = str1[j] - '0';for (i = 0, j = len2 - 1; i < len2; i++, j--)b[i] = str2[j] - '0';int c[2*maxn];memset(c, 0, sizeof(c));for (i = 0; i < len1; i++){for (j = 0; j < len2; j++){c[i + j] += a[i] * b[j];if (c[i + j] >= 10){c[i + j + 1] += c[i + j] / 10;c[i + j] %= 10;}}}i = len1 + len2 - 1;if (c[i] == 0)i--;for (i; i >= 0; i--)cout << c[i];cout << endl;return 0;}


1 0
原创粉丝点击