负数进制

题目描述:
在这里插入图片描述
在这里插入图片描述
代码:

#include<bits/stdc++.h>
using namespace std;
int s[1011]={
    
    0};
int main()
{
    
    
	int m;
	while(cin>>m)
	{
    
    
		int i=0;
		if(m==0) cout<<0<<endl;
		else
		{
    
    
			while(m!=0)			
			{
    
    
				int yushu=abs(m%(-2));//保证余数不为负数
				s[i++]=yushu;
				m=(m-yushu)/(-2);//减去余数后定能整除进制
	     	}
	     	for(int j=i-1;j>=0;--j)
			{
    
    
				cout<<s[j];
			}
			cout<<endl;
		}
		
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43901182/article/details/112531092