Tyvj1022
来源:互联网 发布:淘宝鳄鱼皮真假鉴别 编辑:程序博客网 时间:2024/06/04 01:00
题目链接
分析:
这道题要求我们把十进制数转化成负二进制
如果是二进制,那非常的简单(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;}
阅读全文
0 0