zoukankan      html  css  js  c++  java
  • 十进制转二进制-高速算法

    #include<iostream>
    #include<string>
    #include<algorithm>
    using namespace std;
    int main(int agrc, char *agrv[])
    {
    	int iInPut = 0;
    	while (cin >> iInPut)
    	{
    
    		string sBinary;//转换后的二进制存储为字符串,调用了默认构造函数初试化为空串
    		int temp = abs(iInPut);
    		if (temp == 0)
    		{	
    			//cout.width(11);//以11位的宽度右对齐输出
    			cout << "          0-->0
    ";
    			continue;
    		}
    
    		while (temp)
    		{
    			if (temp & 0x01)
    			{
    				sBinary += '1';
    			}
    			else
    			{
    				sBinary += '0';
    			}
    			temp >>= 1;//对正数右移,高位补0
    		}
    		reverse(sBinary.begin(), sBinary.end());
    		const char *cOutPut = sBinary.c_str();
    		cout.width(11);
    		cout << iInPut << (iInPut > 0 ? "-->" : "-->-") << cOutPut << endl;
    	}
    	return 0;
    }

查看全文
  • 相关阅读:
    动手实现 Redux(六):Redux 总结
    动手实现 Redux(五):不要问为什么的 reducer
    动手实现 Redux(四):共享结构的对象提高性能
    动手实现 Redux(三):纯函数(Pure Function)简介
    动手实现 Redux(二):抽离 store 和监控数据变化
    动手实现 Redux(一):优雅地修改共享状态
    React.js 的 context
    高阶组件(Higher-Order Components)
    实战分析:评论功能(六)
    实战分析:评论功能(五)
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10783856.html
  • Copyright © 2011-2022 走看看