xdoj 1046: 高精度模板综合测试

来源:互联网 发布:telnet端口 登录 内网 编辑:程序博客网 时间:2024/06/07 20:30

题目描述

请输出两个数的和,差,积,商,取余。注意不要有前导零。

输入

多组数据,每组数据是两个整数A,B(0<=A<=10^100,0<B<=10^100).

输出

对于每组数据,输出五个整数,分别代表A+B,A-B,A*B,A/B,A%B.


样例输入

5 211 3

样例输出

7 3 10 2 114 8 33 3 2
#include <iostream>#include <cstring>#include <algorithm>#include <string>#include <cmath>using namespace std;typedef unsigned long ul;string v;int absInt(int a){return a>0?a:a*-1;}//if a>=b return true;bool compare(string a,string b){    if(a[0] == '-')        return false;    if(a.size()>b.size())        return true;    else if(a.size()==b.size())        return a>=b;    return false;}string add(string a,string b){    reverse(a.begin(), a.end());    reverse(b.begin(), b.end());    if(a.size()<b.size())        swap(a, b);    bool f = 0;    for(int i=0;i<b.size();i++)    {        a[i] = a[i] +b[i]-48+f;                if(a[i]>'9')        {            f = true;            a[i] = a[i] -10;        }        else            f = false;    }        for(ul i=b.size();i<a.size();i++)    {        a[i] = a[i]+f;        if(a[i]>'9')        {            f = true;            a[i] = a[i] -10;        }        else            f = false;    }        if(f)        a = a+"1";        reverse(a.begin(),a.end());    return a;    }string sub(string a,string b){    bool flag = compare(a, b);    if(!flag)        swap(a,b);        reverse(a.begin(), a.end());    reverse(b.begin(), b.end());        bool f = false;    for(int i=0;i<b.size();i++)    {        a[i] = a[i] + '0' - b[i] -f;        if(a[i]<'0')        {            f = true;            a[i] = a[i] + 10;        }        else            f = false;    }        for(ul i=b.size();i<a.size();i++)    {        a[i] = a[i] - f;        if(a[i]<'0')        {            f = true;            a[i] = a[i] + 10;        }        else            f = false;    }    reverse(a.begin(),a.end());           while(*a.begin()=='0')        a.erase(a.begin());    if(a.size()==0)        a = "0";         if(!flag)        a = "-"+a;        return a;    }string mul(string a,int b){    int flag = 0;    for(int i=a.size()-1;i>=0;i--)    {        int str = (a[i]-48)*b + flag;        a[i] = str%10 +48;        flag = str/10;    }        if(flag)        a.insert(a.begin(), flag+48);        while(*a.begin()=='0')        a.erase(a.begin());    if(a.size()==0)        a = "0";    return a;}string mul(string a,string b){    string c;    if(a.size()<b.size())        swap(a, b);        c = mul(a,b[b.size()-1]-'0');    string k = "0";    for(int i=b.size()-2;i>=0;i--)    {        string f = mul(a,b[i]-'0');        f = f+k;        c = add(c, f);        k = k + "0";    }        return c;}string div(string a,string b){    string c;    if(a.size()<b.size())    {        c = "0";        v = a;    }        if(a.size()==b.size()&&a<b)    {        c = "0";        v = a;    }        string f(a.begin(),a.begin()+b.size());        int res = 0;        while(compare(f, b))    {        f = sub(f,b);        res++;    }         c.insert(c.end(), res+'0');    for(ul i=b.size();i<a.size();i++)    {        f.insert(f.end(), a[i]);        while(*f.begin()=='0')            f.erase(f.begin());        int res = 0;        while(compare(f, b))        {            f = sub(f,b);            res++;        }        c.insert(c.end(), res+'0');    }       v = f;        while(*c.begin()=='0')        c.erase(c.begin());    if(c.size()==0)        c = "0";    return c;}int main(){        string a,b;    while(cin>>a>>b)    {        cout<<add(a,b)<<' '<<sub(a,b)<<' '<<mul(a,b)<<' '<<div(a, b)<<' '<<v<<endl;    }    return 0;}/*input:5 2output:7 3 10 2 1input:11 3output:14 8 33 3 2*/



//之后会扩展到整数域的计算


1 0
原创粉丝点击