Tyvj1022

xiaoxiao2021-02-28  108

题目链接

分析: 这道题要求我们把十进制数转化成负二进制 如果是二进制,那非常的简单(logn) 负二进制能不能以此类推呢

我们先看几个例子吧 -2 |-13 1 -2 | 7 1 -2 |-3 1 -2 | 2 0 -2 |-1 1 -2 | 1 1 0

-2 |10 0 -2 |-5 1 -2 | 3 1 -2 |-1 1 -2 | 1 1 0

可以看出:

x | p z s | y

x*y+z=p

那我们就把数字和符号分开 来一个大特判就好了

tip

0要特判

这里写代码片 #include<cstdio> #include<cstring> #include<iostream> using namespace std; int n,f; int ans[110],tot=0; int main() { scanf("%d",&n); if (!n) { printf("0"); return 0; } f=1; if (n<0) f=0,n=-n; while (n) { if ((!(n&1))&&!f) //-偶数 { ans[++tot]=0; n=n/2; f=1; } else if ((!(n&1))&&f) //+ { ans[++tot]=0; n=n/2; f=0; } else if ((n&1)&&!f) //- { n=(n+1)/2; ans[++tot]=1; f=1; } else { n=(n-1)/2; f=0; ans[++tot]=1; } } while (!ans[tot]) tot--; for (int i=tot;i>=1;i--) printf("%d",ans[i]); return 0; }
转载请注明原文地址: https://www.6miu.com/read-59260.html

最新回复(0)