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.

题目描述


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;
}



目录
相关文章
|
关系型数据库 PostgreSQL
PostgreSQL 计算字符串字符数函数(CHAR_LENGTH(str))和字符串长度函数(LENGTH(str))
PostgreSQL 计算字符串字符数函数(CHAR_LENGTH(str))和字符串长度函数(LENGTH(str))
2146 0
|
7月前
|
Python
Python字符串和字节不要混淆str.format()和bytes.format()
【5月更文挑战第6天】Python字符串和字节不要混淆str.format()和bytes.format()
50 1
|
7月前
从接口获取获取到数组arr=[‘1‘,‘a‘,‘2‘,‘b‘,‘3‘,‘c‘]转换成{number:‘123’,char:‘abc’}
从接口获取获取到数组arr=[‘1‘,‘a‘,‘2‘,‘b‘,‘3‘,‘c‘]转换成{number:‘123’,char:‘abc’}
|
物联网
CF1506C Double-ended Strings(差不多就是找最长串问题)
CF1506C Double-ended Strings(差不多就是找最长串问题)
49 0
|
C#
C#中 Int32.TryParse() ConVert.ToInt32() Int32.Parse () 的区别 将字符串类型转换为数字类型
C#中 Int32.TryParse() ConVert.ToInt32() Int32.Parse () 的区别 将字符串类型转换为数字类型
66 0
|
Python
Python 数值类型方法|内建函数的对比汇总 (int bool float complex bytes str)
Python 数值类型方法|内建函数的对比汇总 (int bool float complex bytes str)
125 0
|
NoSQL 芯片 Windows
[oeasy]python0045_转化为10进制数_int_integrate_integer_entire_整数
[oeasy]python0045_转化为10进制数_int_integrate_integer_entire_整数
92 0
[oeasy]python0045_转化为10进制数_int_integrate_integer_entire_整数
|
算法
LeetCode 405. Convert a Number to Hexadecimal
给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 方法。
100 0
LeetCode 405. Convert a Number to Hexadecimal