位运算实现加法求助

P1001 A+B Problem

吴锦洋 @ 2020-12-19 17:38:21

#include<cstdio>
#include<iostream>
using namespace std;
int d(int q,int s)
    {int r=s;
    while(q&r)
    {
    q=q^r;  
    r=r<<1;
    }
    q=q|r;
    return q;
    }
int j(int a,int b)
    {
        int q=1;
        while(q<=b)
        {
        if(q&b)
        a=d(a,q); 
        q=q<<1;
        }
        return a;
    }
int main()
{
int a,b;
cin>>a>>b;
cout<<j(a,b);
return 0;
}

by Stinger @ 2020-12-19 17:41:09

删评论珂海星


by 吴锦洋 @ 2020-12-21 13:24:51

好累

#include<cstdio>
#include<iostream>
using namespace std;
int d(int q,int s) {
    int r=s;
    while(q&r) {
        q=q^r;
        r=r<<1;
    }
    q=q|r;
    return q;
}
int j(int a,int b) {
    int q=1;
    while(q<=b) {
        if(q&b)
            a=d(a,q);
        q=q<<1;
    }
    return a;
}
int jj(int a,int b) {
    a=a^b;
    return a;
}
 int cc(int a,int b)
 {
 int g=1,k=0;
    while(b!=a)
    {g=1;
     while(j(j(g,g),b)<=a)
     {
     g=j(g,g);
     } 
     b=j(b,g);
    k=j(k,g);
    }
    return k;
 }
 int xf(int a)
 {if(a>0)
 return ~(cc(a,1));
 else
 if(a<0)
 return cc(0,a);
 }
int main() {
    int a,b,c;
    cin>>a>>b;      
    c=a;
    if(b>a) {
        a=b;
        b=c;
    }
    if(b>=0)
        {
        cout<<j(a,b);
       }
        if(0>a)
        {cout<<xf(j(a,b));
        }
        if(a>=0&&b<0)
        {c=xf(b);
        if(c>a)
        {cout<<xf(cc(c,a));
        }
        else
        {cout<<cc(a,c);
        }
        }
    return 0;
}

|