大整数乘法
来源:互联网 发布:剑三同人知乎 编辑:程序博客网 时间:2024/05/19 03:25
输入为两个100位以内大整数,输出其结果
例程:输入:1234567 123
输出:151851741
本题思想为:用字符串存储其各位字符,以数学乘法基本计算方法进行相乘进位运算,得出最终结果。
#include<iostream>
#include<cmath>#include<cstring>
#include<sstream>
using namespace std;
int main()
{
string str1,str2;
cin>>str1>>str2; //读取两个整数
int wei1=str1.size(),wei2=str2.size();//记录整数位数
string strMid(wei1+wei2,'0');
string strLastRst(wei1+wei2,'0');//大整数乘法结果存储,长度为wei1+wei2
string *strRst=new string[50];//用于存放单次乘积
//strRst[0]="abcde";
for(int i=wei2-1,j=wei1-1; i>=0; i--)
{
int temp=0; //
for(int m=0; m<wei1+wei2; m++)//
strMid[m]='0';//初始化
for(j=wei1-1; j>=0; j--)
{
int a=(str1[j]-'0')*(str2[i]-'0')+temp;
strMid[i+j+1]=a%10+'0';
temp=a/10;
}
strMid[i+j+1]=temp+'0';
strRst[wei2-1-i]=strMid;//strcmp(strRst[wei2-1-i],strMid);//存储一次乘积结果
}
for(int j=wei1+wei2-1; j>=0; j--)
{
static int temp=0;
for(int i=0; i<wei2; i++)
{
temp+=strRst[i][j]-'0';//将wei1次乘积相加
}
strLastRst[j]=temp%10+'0';//并将和的个位存入相应位,十位进位进行下一步运算
temp=temp/10;
}
int x=0;
while(strLastRst[x++] == '0');//避免输出最高位为0的情况
x--;
while(strLastRst[x] != '\0')
cout<<strLastRst[x++];
return 0;
}
- 大整数乘法
- C++大整数乘法
- 大整数FFT乘法
- 大整数乘法
- 大整数乘法
- 大整数乘法
- 大整数乘法
- 大整数乘法。。。。。
- 大整数乘法
- 大整数乘法
- // //大整数乘法
- 大整数乘法
- 大整数乘法算法
- 【算法】 大整数乘法
- 大整数乘法
- 大整数乘法
- 大整数乘法。
- 大整数乘法
- Java IO系统
- WIN7环境安装kali双系统
- 程序员如何找对象(1)
- C# 沙漏源码
- JAVA图形用户界面编程及事件处理
- 大整数乘法
- 快速获取数列/矩阵中前项元素累积和
- React-Native环境配置之安装Node.js
- 尝试做事情30天-架构即未来
- 专业化基础—— JAVA基本了解
- 数据库
- Codeforces Round #346 (Div. 2) F. Polycarp and Hay (并查集+bfs)★
- Codeforces 363B Fence-暴力
- 暑期社会实践心得