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



目录
相关文章
|
9月前
|
C#
C#中 Int32.TryParse() ConVert.ToInt32() Int32.Parse () 的区别 将字符串类型转换为数字类型
C#中 Int32.TryParse() ConVert.ToInt32() Int32.Parse () 的区别 将字符串类型转换为数字类型
35 0
|
11月前
|
Python
Python 数值类型方法|内建函数的对比汇总 (int bool float complex bytes str)
Python 数值类型方法|内建函数的对比汇总 (int bool float complex bytes str)
87 0
|
算法
LeetCode 405. Convert a Number to Hexadecimal
给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 方法。
65 0
LeetCode 405. Convert a Number to Hexadecimal
LeetCode 136. 只出现一次的数字 Single Number
LeetCode 136. 只出现一次的数字 Single Number
HDOJ(HDU) 2106 decimal system(进制相互转换问题)
HDOJ(HDU) 2106 decimal system(进制相互转换问题)
69 0
LeetCode 136:只出现一次的数字 Single Number
题目: 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。 Given a non-empty array of integers, every element appears twice except for one. Find that single one. 说明: 你的算法应该具有线性时间复杂度。
952 0