鸽兔同校

//就是大数的处理,java的话就很简单了
/*x+y=n  2x+4y=m
y=(m-2n)/2=0.5m-n
x=2n-0.5m

*/
#include<iostream>
using namespace std;
//求大数的一半
string half(string s){
    int borrow=0;
    int len=s.length();
    string ret(len,'0');
    int i=0;
    while(i<len){
        ret[i]=(borrow*10+s[i]-'0')/2+'0';
        borrow=(s[i]-'0')%2;
    //  cout<<ret[i]<<endl;
        ++i;
    }
    //删掉第一个0
    if(ret[0]=='0')
        ret.erase(0,1);
    return ret;
}
//求大树的两倍
string doub(string s){
    int carry=0;
    int len=s.length();
    string ret(len+1,'0');
    int i=len;
    while(i>0){
        ret[i]=((s[i-1]-'0')*2+carry)%10+'0';
        carry=((s[i-1]-'0')*2+carry)/10;
        --i;
    }
    if(carry==1)
        ret[0]='1';
    else
        ret.erase(0,1);
    return ret;
}
//两个大数相减
string sub(string s1,string s2){
    int i,j,borrow;
    string r1;
    string r2;
    if(s1.length()!=s2.length()){
        r1=s1.length()>s2.length()?s1:s2;
        r2=s1.length()<s2.length()?s1:s2;
        i=r1.length()-r2.length();
        //不一样长,前面补0对齐
        while(i>0){
            r2.insert(0,"0");
            i--;
        }
    }
    else{
        r1=s1;
        r2=s2;
    }
    i=r1.length()-1;
    string ret(r1.length(),'0');
    borrow=0;
    while(i>=0){
        if(r1[i]<r2[i]){
            ret[i]=10+r1[i]-r2[i]+'0'-borrow;
            borrow=1;
        }
        else if(r1[i]==r2[i]&&borrow==0){
            ret[i]='0';
        }
        else if(r1[i]==r2[i]&&borrow!=0){
            ret[i]='9';
        }
        else{
            ret[i]=r1[i]-r2[i]+'0'-borrow;
            borrow=0;
        }
        //cout<<ret[i]<<endl;
        --i;
    }
    while(ret[0]=='0')
        ret.erase(0,1);
    return ret;
}
bool check(string a,string b){
    if(a.size()>b.size())
        return true;
    else if(a.size()==b.size())
        return a.compare(b)>0;
    else return false;
}
int main(){
    string n,m;
    string x,y;
    string cn,cm;
    //cout<<half("702835084")<<endl;
    //cout<<doub("311019898")<<endl;
    //cout<<sub(half("702835084"),"311019898")<<endl;
    while(cin>>n>>m){
        x=sub(doub(n),half(m));
        y=sub(half(m),n);

        cn=sub(sub(n,x),y);
        cm=sub(sub(m,doub(x)),doub(doub(y)));
        if(!check(doub(n),half(m))||!check(half(m),n)) {
                cout<<"Error"<<endl;
        }
        else if(cn.length()==0&&cm.length()==0)
            cout<<x<<" "<<y<<endl;
        else
            cout<<"Error"<<endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值