码迷,mamicode.com
首页 > 其他好文 > 详细

大整数乘法

时间:2018-10-01 19:55:18      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:std   处理   char   删除   names   using   大整数   i++   space   

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>   
using namespace std;  
const int maxn = 2000;
int a[maxn];
int b[maxn];
int c[maxn];
char s[maxn];
char t[maxn];
int x;
int main(void)  
{  
    cin>>s;//输入乘数str1  
    cin>>t;//输入乘数str2  
    int lens = strlen(s);  
    int lent = strlen(t);  
    for(int i = 0; i <= lens-1; i++)//乘数s存入数组a  
        a[lens-i] = s[i]-0;  
    for(int i = 0; i <= lent-1; i++)//乘数t存入数组b  
        b[lent-i] = t[i]-0;  
    for(int i = 1; i <= lens; i++)  
    {  
        x=0;//用于存放进位  
        for(int j = 1; j <= lent; j++)//对乘数每一位进行处理  
        {  
            c[i+j-1]=a[j]*b[i]+x+c[i+j-1];//当前乘积+上次乘积进位+原数  
            x=c[i+j-1]/10;  
            c[i+j-1]%=10;  
        }  
        c[i+lent]=x;//进位  
    }  
    int lenc=lens+lent;  
    while(c[lenc]==0)//删除前导0  
        lenc--;  
    for(int i  = lenc; i >= 1; i--)//倒序输出  
        cout<<c[i];  
    cout<<endl;  
    return 0;  
}  

 

大整数乘法

标签:std   处理   char   删除   names   using   大整数   i++   space   

原文地址:https://www.cnblogs.com/AC-AC/p/9735336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!