编程之美--2.4 1的数目之扩展问题
来源:互联网 发布:8888端口干嘛的 编辑:程序博客网 时间:2024/05/18 02:58
思路与书中2.4的类似,只是把十进制换成了二进制,只需在iFactor*10的地方,改成iFactor*2即可。
代码如下:
#include <iostream>#include<bitset>using namespace std;const unsigned SYSTEM = 2;unsigned long Sum1s(unsigned long n){unsigned long iCount = 0;unsigned long iFactor = 1;unsigned long iLowerNum = 0;unsigned long iCurrNum = 0;unsigned long iHigherNum = 0;while (n / iFactor != 0){iLowerNum = n - (n / iFactor) * iFactor;iCurrNum = (n / iFactor) % SYSTEM;iHigherNum = n / (iFactor * SYSTEM);switch (iCurrNum){case 0:iCount += iHigherNum * iFactor;break;case 1:iCount += iHigherNum * iFactor + iLowerNum + 1;break;default:iCount += (iHigherNum + 1) * iFactor;break;}iFactor *= SYSTEM;}return iCount;}int main(){unsigned long n = 0x4;cout << "binary number: " << bitset<8>(n) << endl;cout << "number of 1: " << Sum1s(n) << endl;system("pause");return 0;}
0 0
- 编程之美--2.4 1的数目之扩展问题
- 编程之美 2.4 “1”的数目及扩展问题
- 编程之美 2.4 “1”的数目
- 编程之美 2.4 1的数目
- 编程之美 2.4 “1”的数目
- 编程之美2.4 1的数目
- 编程之美-2.4 1的数目
- [编程之美2.4]1的数目
- 编程之美 2.4 1的数目
- 编程之美2.4 1的数目
- 编程之美--2.4 1的数目
- 编程之美 2.4 1的数目
- 编程之美 2.4 1的数目
- 编程之美2.4 1的数目
- 编程之美之1的数目
- 编程之美之1的数目
- 读书笔记之编程之美 - 2.4 1的数目
- 编程之美---求1的数目
- 获取样式方法
- 数据竞赛利器 —— xgboost 学习清单
- (五)Java程序设计之浅谈Arrays类
- Git push远程仓库总是需要账号密码?
- ZJNU 1903 Why Did the Cow Cross the Road III 树状数组
- 编程之美--2.4 1的数目之扩展问题
- Andrew NG机器学习课程笔记系列之——机器学习之逻辑回归(Logistic Regression)
- rtmp握手过程
- JPA主键生成器和主键生成策略
- 分治算法 ———— 排序问题
- MFC中利用FindFile函数在文件夹路径下连续读取多个文件
- java 7新特性-TWR(Try-with-resources)
- AJAX 初步学习(1)
- linux环境Mysql 5.7.13安装教程