鸽子兔子

来源:互联网 发布:windows 10.1新版推送 编辑:程序博客网 时间:2024/05/17 06:28
//就是大数的处理,java的话就很简单了/*x+y=n  2x+4y=my=(m-2n)/2=0.5m-nx=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
原创粉丝点击