【BZOJ3767】A+B Problem加强版

3767: A+B Problem加强版

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 760  Solved: 339
[Submit][Status][Discuss]

Description

 

Input

输入A,B
 

Output

输出A+B。

 

Sample Input

1 1

Sample Output

2

HINT

 

对于100%的数据,保证 |A| , |B| 不会超过10^(10^7)

 

sol:虽然说不支持提交了但是还是能交

压位板子题1A了

/*To The End Of The Galaxy*/
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iomanip>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<complex>
#define debug(x) cerr<<#x<<"="<<x<<endl
#define INF 0x7f7f7f7f
#define llINF 0x7fffffffffffll
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
inline int init()
{
    int now=0,ju=1;char c;bool flag=false;
    while(1)
    {
        c=getchar();
        if(c=='-')ju=-1;
        else if(c>='0'&&c<='9')
        {
            now=now*10+c-'0';
            flag=true;
        }
        else if(flag)return now*ju;
    }
}
inline long long llinit()
{
    long long now=0,ju=1;char c;bool flag=false;
    while(1)
    {
        c=getchar();
        if(c=='-')ju=-1;
        else if(c>='0'&&c<='9')
        {
            now=now*10+c-'0';
            flag=true;
        }
        else if(flag)return now*ju;
    }
}
const int inc=100000000;
const int p=8;
struct bignumber
{
    int num[100001];
    int len;
}a,b,ans;
char temp[10];
char x[10000001],y[10000001];
bignumber l; 
bignumber make(int len,char tmp[])
{
    bignumber ans=l;
    int cnt=0,c=0;
    memset(temp,0,sizeof(temp));
    for(int i=1;i<=(len/2);i++)
    {
        swap(tmp[i],tmp[len-i+1]);
    }
    for(int i=1;i<=len;i++)
    {
        temp[++cnt]=tmp[i];
        if(cnt==p)
        {
            for(int i=1;i<=(cnt/2);i++)
            {
                swap(temp[i],temp[p-i+1]);
            }
            cnt=0;
            ans.len++;
            ans.num[ans.len]=atoi(temp+1);
            memset(temp,0,sizeof(temp));
        }
    }
    if(cnt!=0)
    {
        for(int i=1;i<=(cnt/2);i++)
        {
            swap(temp[i],temp[cnt-i+1]);
        }
        ans.len++;
        ans.num[ans.len]=atoi(temp+1);
    }
    return ans;
}
bignumber operator + (bignumber a,bignumber b)
{
    int len=max(a.len,b.len);
    a.len=len;
    for(int i=1;i<=len;i++)
    {
        a.num[i]+=b.num[i];
    }
    for(int i=1;i<len;i++)
    {
        a.num[i+1]+=a.num[i]/inc;
        a.num[i]%=inc;
    }
    if(a.num[len]>=inc)
    {
        a.num[len+1]+=a.num[len]/inc;
        a.num[len]%=inc;
        a.len++;
    }
    return a;
}
bignumber operator - (bignumber a,bignumber b)
{
    int c=0;
    int len=max(a.len,b.len);
    a.len=len;
    for(int i=1;i<=len;i++)
    {
        a.num[i]-=b.num[i];
    }
    for(int i=1;i<len;i++)
    {
        c=0;
        if(a.num[i]<0)
        {
            a.num[i]+=inc;
            c=1;
        }
        a.num[i+1]-=c;
    }
    if(a.num[len]<=0)a.len--;
    return a;
}
void print(bignumber x)
{
    printf("%d",x.num[x.len]);
    for(int i=x.len-1;i>=1;i--)
    {
        printf("%08d",x.num[i]);
    }
    return;
}
bool operator > (bignumber a,bignumber b)
{
    if(a.len>b.len)return a.len>b.len;
    else 
    {
        for(int i=a.len;i>=1;i--)
        {
            if(a.num[i]>b.num[i])return true;
            else return false;
        }
    }
}
int main()
{
    //freopen("ab.in","r",stdin);
    //freopen("ab.out","w",stdout);
    scanf("%s%s",x+1,y+1);
    int t1=0,t2=0;
    if(x[1]=='-')t1=1;
    a=make(strlen(x+t1+1),x+t1);
    if(y[1]=='-')t2=1;
    b=make(strlen(y+t2+1),y+t2);
    if(t1^t2)
    {
        if(b>a)
        {
            swap(a,b);
            swap(t1,t2);
        }
        ans=a-b;
        if(t1)printf("-");
    }
    else
    {
        ans=a+b;
        if(t1==1&&t2==1)printf("-");
    }
    print(ans);
    return 0;
}
View Code

 

posted @ 2017-03-15 10:50  redwind  阅读(337)  评论(0编辑  收藏  举报