Problem E: Product
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 18 Solved: 14
[Submit][Status][Web Board]
Description
The problem is to multiply two integers X, Y. (0<=X,Y<10250)
Input
The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.
Output
For each input pair of lines the output line should consist one integer the product.
Sample Input
12
12
2
222222222222222222222222
Sample Output
144
444444444444444444444444
my answer:
#include<iostream> #include<cstring> #include<string> using namespace std; int main() { char a[100]; char b[100]; memset(a,'0',sizeof(a)); memset(b,'0',sizeof(b)); while(cin>>a>>b) { int sum[101]={0}; int t1=strlen(a); int q=t1; int t2=strlen(b); for(int i=t2-1;t2>0,i>=0;i--){ int p=100; for(int j=q-1,p=100+i-t2+1;j>=0;t1--){ sum[p--]+=((a[j--]-'0')*(b[i]-'0')); } } for(int k=100;k>=0;k--){ sum[k-1]+=sum[k]/10; sum[k]=sum[k]%10; } int start=0; while(!sum[start]) start++; for(int j=start;j<=100;j++) cout<<sum[j]; cout<<endl; } return 0; }
本文详细介绍了如何使用C++实现两个大数之间的乘法运算,通过字符串存储大数并逐位相乘的方式,解决了传统整型数据溢出的问题。算法适用于超过标准整型变量范围的大数计算。
561

被折叠的 条评论
为什么被折叠?



