Questions:Given a decimal integer n, write down all numbers starting from 1 to N, and then count the number of 1.
For example n = 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
The number of 1 is 9.
Analysis: The first way to compare violence, a number for a number of checks. Complexity of N (log2n)
The second way to find the law, as in the case of n=16:-The first is a single digit of 1:1, 112-The second is 10 bit on the 1:10, 11, 12,13,14,15,16 Seven
1:9 x
Again like n = 23-The first is on the single digit 1:1, 11, 213-The second is 10-1:10
1:13 x
So if you increment one on the ten, then the 1 on the single digit will be added one, and the 10 digit number determines the digit number if the number on the ten is a 1, or greater than 1, then the number of the number of single digits is determined by the numbers of the ten 1.
Again like N = 123-The first one is on the single digit of 1:1, 11,21 ... 121 0, 10, 20, .... 120 13-The second one is 10 bits on 1:10 ~ 19, 110 ~ 119 20-The third one is the Hundred 1:100 ~ 123, 24
1:13 + 20 + 24 = 57
According to the number of digits, 1 per bit is related to its highs and lows. Assuming the current bit: C High: H low value: L STEP:10 Progress
if (c = = 1)//1 The number and high and low are related to CNT + = h* (STEP/10) + L + 1 else if (c = = 0)//1 The number and the high has a relationship cnt + = h* (step/1 0) Else//current bit greater than 1 case, and high relationship cnt + = (h+1) * (STEP/10) +1
Instance Program
#include <iostream> using namespace std;
int calc (int N) {int ncnt = 0;
int nh=0, nc=0, nl=0;
int nstep = 10;
cout << "n=" << N << endl << Endl;
while (NSTEP/10 <= N) {NC = (n% nstep)/(NSTEP/10);
NH = N/nstep;
NL = (N nstep)-NC * (NSTEP/10); cout << "c=" << nC << "h=" << nH << "l=" << nL << "step=" << nste
P/10 << Endl;
Switch (NC) {case 0:ncnt + = nh* (NSTEP/10);
Break
Case 1:ncnt + = nh* (NSTEP/10) + nl+1;
Break
default:ncnt + = (nh+1) * (NSTEP/10);
Break
} nstep *= 10;
} cout << "count=" << ncnt << Endl;
cout << "=====================================\n" <<endl;
return ncnt;
} int main () {int nret = 0, i = 0; int test[] = {19,0,1,10,23,123,223,1023};
int len = sizeof (test)/sizeof (test[0]);
for (i = 0; i < len; i++) {nret = Calc (test[i]);
} cin >> Nret;
return 0;
}
Scaling Issues
Binary number, with the following law f (1) = 1 (1 has a 1) f (10) = 10 (01, 10 have 2 1) f (11) = 100 (01 10 11 have 4 1)
The function f () is to find out how many 1 of binary numbers exist.
Extended Problem Analysis
You can start with a full 1 scenario: f (1) = 1 (with 1 bits k = 1) f (11) = 4 (with 2 bits k = 2)
F (111) = 12 (with 3-bit k = 3)
So you can get a full 1 o'clock Formula K * 2^ (k-1) k is a total number of digits then a binary number 1 can be split into two parts, minus the highest bit 1, the remaining (k-1) bit of the total 1 number + plus the highest bit of 1 after the remaining number
For example: 10010 can be divided into two parts 1111 of 1 and + 10000 to 10010 of 1 and from 10000 to 10010 can also continue to split:
1 0010 the number of 1 of the highest bit is determined by the back of the low, such as three to 1 0000, 1 0001, 1 0010 red parts have three. -0010 low also need to continue to follow the rules of the above iteration, until the end of 0001 102 with F (10)
To extend the problem code:
#include <iostream> using namespace std; /* First calculate the count of 1 in a full 1 number eg:1 = CNT = 1; CNT = 4;
111 = (001 010 011 101) CNT = the We 111 cnt = k * GOT (2^) */int Calc (k-1 binary, int len) {
int ncnt = 1, num = 0;
int k = len, total = 0;
char* p = binary+1;
if (len <= 0) return 0;
while (K > 1) {if (binary[k-1]== ' 1 ') num + = ncnt;
ncnt *= 2;
k--;
} if (num = = (nCnt-1))//full of 1 return total = len*ncnt;
else total = (len-1) * (NCNT/2);
Total + = num+1;
K = 1;
while (*p! = ' 1 ') && (K<len)) {*p++; k++;
} Total + = Calc (p, len-k);
return total; } int main () {char* test[]= {"100", "1", "10", "11", "111", "10000", "1111", "10110", "110011", "
110011001 "};
int test_num = 10;
int len = 0, i = 0, total=0; FoR (i = 0; i < Test_num; i++) {len = strlen (Test[i]);
Total = Calc (test[i], Len);
cout << Test[i] << ": result=" << total << Endl;
cout << "\ n =========================\n" << Endl;
} cin >> Len;
return 0; }