1 #include <iostream>
2 using namespace std;
3
4 #include <stdlib.h>
5 #include <sys/types.h>
6
7 int CountOf1(u_int32_t n)
8 {
9 u_int32_t lower = 0;
10 u_int32_t cur = 0;
11 u_int32_t upper = 0;
12 int count = 0;
13 u_int32_t factor = 1;
14
15 while (n / factor != 0) {
16 lower = n % factor;
17 upper = n / (10 * factor);
18 cur = (n % (10 * factor))/ factor;
19 switch(cur) {
20 case 0:
21 count += upper * factor;
22 break;
23 case 1:
24 count += (upper * factor + lower + 1);
25 break;
26 default:
27 count += (upper + 1) * factor;
28 }
29
30 factor *= 10;
31 }
32
33 return count;
34 }
35
36 int main()
37 {
38 cout << CountOf1(11u) << endl;
39
40 return 0;
41 }
posted on 2011-10-22 10:31  吃吃户  阅读(147)  评论(0编辑  收藏  举报