题目链接:http://ac.jobdu.com/problem.php?pid=1037
(微难,调了好久,苦难才刚刚开始,加油!)
题目分析:
由于数字位数太大,直接计算一定不行。用Java可以简单实现,但是看下面截图(
请注意耗时和内存)
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
BigInteger a,b;
Scanner cin=new Scanner(new BufferedInputStream(System.in));
while(cin.hasNext()){
a=cin.nextBigInteger();
b=cin.nextBigInteger();
System.out.println(a.add(b));
System.out.println(a.subtract(b));
System.out.println(a.multiply(b));
}
}
}
主要介绍c++实现:
首先解释两个函数:
reverse(s.begin,s.end):倒置函数。即若s为“123456”,则倒置后为“654321”;
resize(size):字符串设置大小;
push_back:vector在push_back的时候,如果空间不足,会自动增补一些空间,如果没有预留的空间可用
就直接申请另一块可用的连续的空间,把数据拷贝过去。
难点:将数字看成字符串,加减乘运算的算法设计,将字符串倒置运算和最后的输出。
加法:注意进位,和最后一位的计算
减法:注意结果的正负,注意借位的使用
乘法:算法的设计
输出:根据不同运算的结果,输出要分情况。这里着重考虑0*一个数,和A-A=0情况。
代码如下:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string add(string aa,string bb)
{
string x,y,z;
x = aa;
y = bb;
int la,lb,lc,ztmp;
la = x.length();
lb = y.length();
reverse(x.begin(),x.end());//倒置函数
reverse(y.begin(),y.end());
int jinwei=0;
lc = la>lb?la:lb;
//int flag=0;
z.resize(0);
for(int i=0;i<lc;i++)
{
if(i<la&&i<lb)
{
ztmp = jinwei + (x[i]-'0')+(y[i]-'0');
jinwei = ztmp/10;
}
else if(i>=la&&i<lb)
{
ztmp = jinwei + (y[i]-'0');
jinwei = ztmp/10;
}
else if(i>=lb&&i<la)
{
ztmp = jinwei + (x[i]-'0');
jinwei = ztmp/10;
}
z.push_back('0' + ztmp%10);
if(i==lc-1){
z.push_back(jinwei + '0');
jinwei = 0;
}
}
reverse(z.begin(),z.end());
return z;
}
string sub(string a,string b)
{
string x,y,z;
x=a;
y=b;
reverse(x.begin(),x.end());
reverse(y.begin(),y.end());
z.resize(0);
int borrow = 0;
bool sign = false;
int tmp;
if(x.length()<y.length())
{
swap(x,y);
sign = true;
}
else if(x.length() == y.length())
{
for(int i= x.length()-1;i>=0;i--)
{
if(x[i]<y[i])
{
swap(x,y);
sign = true;
break;
}
}
}
for(int i=0;i<x.length();i++)
{
if(i<y.length())
{
tmp = (x[i]-'0')-(y[i]-'0') - borrow;
if(tmp<0)
{
tmp = tmp + 10;
borrow = 1;
z.push_back('0'+tmp);
}
else
{
z.push_back('0'+tmp);
borrow = 0;
}
}
else
{
//cout<<x[2];
tmp = (x[i] - '0') - borrow;
if(tmp<0)
{
tmp = tmp + 10;
borrow = 1;
z.push_back('0'+tmp);
}
else
{
z.push_back('0'+tmp);
borrow = 0;
}
}
}
if(sign)
{
z = z + '-';
}
reverse(z.begin(),z.end());
return z;
}
string mul(string a,string b)
{
string x,y,z;
x = a;
y = b;
int la,lb;
la = x.length();
lb = y.length();
z.resize(la+lb);
for(int i=0;i<z.length();i++)
{
z[i] = '0';
}
int jinwei =0,tmp;
reverse(x.begin(),x.end());
reverse(y.begin(),y.end());
for(int i=0;i<la;i++)
{
for(int j=0;j<lb;j++)
{
tmp = jinwei +(x[i]-'0')*(y[j]-'0') + (z[i+j]-'0');
z[i+j] = '0' + tmp%10;
jinwei = tmp/10;
if(j==lb-1)
{
z[i+j+1] = '0' + jinwei;
jinwei = 0;
}
}
}
reverse(z.begin(),z.end());
return z;
}
void printf(string c)
{
if(c[0]=='-')
{
cout<<c[0];
for(int i=1;i<c.length();i++)
{
cout<<c[i];
}
}
else if(c[0]=='0'&&c.length()==1)
{
cout<<c[0];
}
else if(c[0] == '0'&&c[1]=='0')
{
cout<<'0';
}
else if(c[0] == '0')
{
for(int i=1;i<c.length();i++)
{
cout<<c[i];
}
}
else if(c[0]!=0)
{
cout<<c[0];
for(int i=1;i<c.length();i++)
{
cout<<c[i];
}
}
cout<<endl;
}
int main()
{
string a,b,c;
while(cin>>a>>b)
{
c = add(a,b);
printf(c);
c = sub(a,b);
printf(c);
c = mul(a,b);
printf(c);
}
return 0;
}