当前位置: 代码迷 >> 综合 >> poj 3191 The Moronic Cowmpouter 十进制转-2进制
  详细解决方案

poj 3191 The Moronic Cowmpouter 十进制转-2进制

热度:62   发布时间:2024-01-19 06:26:29.0

题意:

给一个10进制的数。把他转化为-2进制。

思路:

水题,直接边除边模即可。

代码:

#include <iostream>
using namespace std;
int a[64];
int main()
{int cnt=0,n;scanf("%d",&n);	while(n){int x=n/-2;int y=n%-2;if(y<0){y+=2;x+=1;}a[++cnt]=y;			n=x;}if(cnt==0)printf("0");else{for(int i=cnt;i>=1;--i)printf("%d",a[i]);}return 0;	
}