大整数乘法

大整数乘法

 

Time Limit:   5000MS       Memory Limit:   65535KB
Submissions:   1757       Accepted:   349

 

Description
在一些应用中,特别是现在的密码学中,常常需要用超过100位的整数来做乘法,以此来对数据加密。现在有两个小于等于100位的大整数a和b(位数相同),请写程序计算出这两个大整数乘积的结果。
Input
输入有三行第一行是大整数位数第一个大整数第二个大整数
Output
两个大整数的结果

Sample Input

10
1111111111
1111111111

Sample Output

1234567900987654321

# include<iostream>
# include<cstring>
using namespace std;
int str[300];
int main()
{
    int nLen,flag=0;
    char s1[120],s2[120];
    int i,j,k;
    cin>>nLen;
    cin>>s1>>s2;
    memset(str,0,sizeof(str));
    for(i=nLen-1;i>=0;i--)//注意从nLen-1开始
    {
        k=nLen-i-1;
        for(j=nLen-1;j>=0;j--,k++)
        {
            str[k]+=(s1[i]-'0')*(s2[j]-'0');
            if(str[k]>9)str[k+1]+=str[k]/10;
            str[k]%=10;
        }
        if(str[k]>9)str[k+1]+=str[k]/10;
            str[k]%=10;
    }
    for(i=k;i>=0;i--)
    {
        if(flag==0)
        {
        if(str[i]!=0)
        {
            cout<<str[i];
            flag=1;
        }
        }
        else
            cout<<str[i];
    }
    if(!flag)cout<<"0";//注意0
    cout<<endl;
    return 0;
}

 

posted on 2012-05-21 22:21  即为将军  阅读(291)  评论(0)    收藏  举报

导航