count the number of 1 in a decimal integer
2013-08-07 12:00
393 查看
#include <iostream> using namespace std; long count1sOfInteger(int num) { long res = 0; int factor = 1; long high, cur, low; while(num / factor) { low = num - (num/factor)*factor;//num % factor; cur = (num/factor) % 10; high = (num/factor) / 10; switch(cur) { case 0: res += (high*factor); break; case 1: res += (high*factor) + (low+1); break; default: res += (high+1) * factor; break; } factor *= 10; } return res; } int main() { int num; while(cin >> num) { int count = count1sOfInteger(num); cout << count << endl; } //cout << "Hello world!" << endl; return 0; }
相关文章推荐
- Count the number of bits that are on in an unsigned integer(计算一个无符整数中1Bit的个数)-- (1)
- 在排序数组中的出现次数 Count the number of occurrences in a sorted array
- Distinguish between index of a decimal number and integer inside an array in Ruby?
- Number of Bits in a Decimal Integer
- Give a very good method to count the number of ones in a "n" (e.g. 32) bit number.
- Counting-the-number-of-set-bits-in-an-integer
- Count the number of occurrences in a sorted array
- [geeksforgeeks] Count the number of occurrences in a sorted array
- count the number of "1" in the binary figure
- quick count the number of the rows in a big table
- Count the number of alphabets, spaces, digits and other characters in one line character
- How to count the number of threads in a process on Linux
- computing the number of leading zeros in a word
- the number of surfers in Guilin
- Float and the form of expression of floating-point number in memory
- [LintCode] Number of Airplanes in the Sky
- proxool--高并发异常:We are already in the process of making 11 connections and the number of simultaneous
- How to calculate the number of parameters in CNNs?
- 解决 Error:The number of method references in a .dex file cannot exceed 64K. Learn how to resolve this
- Error:The number of method references in a .dex file cannot exceed 64K 问题解决