首页 > 其他 > 详细

【洛谷P1303 A*B Problem】

时间:2019-03-03 11:13:14      阅读:153      评论:0      收藏:0      [点我收藏+]

题目描述

求两数的积。

输入输出格式

输入格式:

 

两行,两个数。

 

输出格式:

 

 

输入输出样例

输入样例#1: 
1 
2
输出样例#1: 
2

说明

每个数字不超过10^2000,需用高精

emm,显然本题需要用高精:高精,顾名思义,高精度运算,简洁来说就是很大的数字进行运算。

乘法中也运用了一些思想;比如说对于进位的处理(我用的是x)

先看看代码吧

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

int m[2005];
int n[2005];
int ans[4005]={0};

int main()
{
string a,b;
cin>>a>>b;
int length1=a.length();
int length2=b.length();
int x;
for(int i=1;i<=length1;i++)
{
m[i]=a[length1-i]-‘0‘;
}
for(int i=1;i<=length2;i++)
{
n[i]=b[length2-i]-‘0‘;
}
for(int i=1;i<=length1;i++)
{
x=0;
for(int j=1;j<=length2;j++)
{
int t=m[i]*n[j];
ans[i+j-1]+=x+t;
x=ans[i+j-1]/10;
ans[i+j-1]%=10;
}
ans[i+length2]=x;
}
int length=length1+length2;
while(ans[length]==0&&length>1)
{
length--;
}
for(int i=length;i>=1;i--)
cout<<ans[i];
cout<<endl;
return 0;
}

这就是比较传统的思想,对于进位的处理相对来说较容易理解(但代码有些麻烦)

通过熟练后也可以根据自己的码风来进行修改

【洛谷P1303 A*B Problem】

原文:https://www.cnblogs.com/gongcheng456/p/10464297.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!