base -2 Number——进制转换

题目描述

Given an integer N, find the base −2 representation of N.
Here, S is the base −2 representation of N when the following are all satisfied:
S is a string consisting of 0 and 1.
Unless S= 0, the initial character of S is 1.
Let S=SkSk−1…S0, then S0×(−2)0+S1×(−2)1+…+Sk×(−2)k=N.
It can be proved that, for any integer M, the base −2 representation of M is uniquely determined.

Constraints
·Every value in input is integer.
·−109≤N≤109

输入

Input is given from Standard Input in the following format:
N

输出

Print the base −2 representation of N.

样例输入

-9

样例输出

1011

提示

As (−2)0+(−2)1+(−2)3=1+(−2)+(−8)=−9, 1011 is the base −2 representation of −9.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%分界线

首先说明这个题就是进制转换,思路基本和之前的进制转换的思路差不多,就是对-2进行取余

#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize (2)
#pragma G++ optimize (2)
#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
//#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
const ll inf = 1e15;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
#define start int wuyt()
#define end return 0
#define N 1005
int num[maxn];
///int sum[maxn];
map<ll,ll>mp;
int n;
ll m;
start{
    ll n=read;
    int cnt=0;
    do{
        cnt++; num[cnt]=abs(n%2); n=(n-num[cnt])/(-2);
    }
    while(n!=0);
    for(int i=cnt;i>0;i--)
        printf("%d",num[i]);
    putchar('\n');
	end;
}

posted @ 2020-03-15 00:05  PushyTao  阅读(158)  评论(0编辑  收藏  举报