AtCoder Beginner Contest 105 C - Base -2 Number [负进制转换]

版权声明:QQ764229539,欢迎指正! https://blog.csdn.net/Haipai1998/article/details/82017384

AtCoder Beginner Contest 105 C - Base -2 Numbear

题意 : 给定一个n , 转换成 -2 进制

#include<cstdio>
#include<vector>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+5;
const int MOD=1e9+7;
const double EPS=1e-12;
template <class T>
bool sf(T &ret){ //Faster Input
    char c; int sgn; T bit=0.1;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    if(c==' '||c=='\n'){ ret*=sgn; return 1; }
    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
    ret*=sgn;
    return 1;
}
int sign(double x){
    return fabs(x) < EPS ? 0 : x>0 ? 1 : -1 ;
}

int main(void){
    ll n;
    sf(n);
    if(n==0){
        cout << 0 << endl;
        return 0;
    }
    string ans="";
    while(n){
        ll t=n%(-2);
        n/=(-2);
        if(t<0) t+=2,n++;// 记得++
        ans+='0'+t;
    }
    reverse(ans.begin(),ans.end());
    cout << ans << endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Haipai1998/article/details/82017384