The topics are as follows:
The task is simple:given any positive an integer N, you're supposed to count the total number of 1 's in the decimal form of The integers from 1 to N. For example, given N being, there is five 1 ' in 1, ten, one, and 12.
Input Specification:
Each of the input file contains one test case which gives the positive N (<=2).
Output Specification:
For each test case, print the number of 1 's in one line.
Sample Input:
12
Sample Output:
5
For this kind of problem, it should be based on the rule of seeking generalization, but should not try to deduce the mathematical expression of sorting combination.
The difficulty of this problem is that each person has 1 possible, such as 100,010,001,110, if the combination of the order to consider, because to count the number of 1, so for the number of 1 is different, to be treated separately, this will be a tricky issue.
If you change your mind, starting with each one, studying the laws of each one will make the problem much simpler, and this method of research is scientific, because each one considers his own 1, then it is natural to consider the problem of more than 1, for example N=12, we consider the following situation separately:
Single-digit 1 has 1, 11, 10 bits appear 1 of the 10,11,12, we see, for 11 This multiple 1 of the situation, just calculate two times, for many is the same reason.
Let's start with a 5-digit number, and study the calculation of all cases where hundreds are different when the Hundred is 1:
We take 12045, 12145, 12245 as an example.
12045:
The Hundred is 0, then the high-level constant low part can not produce a number similar to 1XX, so consider high, notice as long as the current value is small, so you can choose: 00100-00199,01100-01199 ... 11100-11199 altogether 12*100=1200, observed to be equal to the number *100 than the current high.
That is: high-level digital *100, Note that this 100 is related to the current consideration, the hundred is 100, from the low of the 1th bit to calculate the X-position of x 10, that is, the different bits of this 100 should be replaced by the corresponding value .
12145:
Hundred is 1, then the high-level unchanged, low from 100-145 can be, a total of 45+1=46, high can still produce hundreds of 0 o'clock changes, a total of 1200+46+1246 species
That is: high digit *100 + low number +1
12245:
For the hundred more than 1, we only need to consider the high can be listed all, from 00100-00199 to 12100-12199, total 13*100=1300
That is: (high number + 1) *100
Solve this problem, the following to solve is to remove the high number, the current bit, low number of the problem, we take 5-digit ABCDE to remove the hundred as an example:
For abcdef, set factor factor=100, that is, to remove the hundred D
By conventional method, the Hundred = abcdef/factor% = abcd% = d
High number = ABCDEF/FACTOR/10 = ABC
Low number = ABCDEF-ABCD00 = EF
and abcd00 = (abcdef/factor) * factor = abcd00
So the low number = abcdef-(abcdef/factor) * factor
At this time, factor is the first we are looking for from the beginning of the 10 x, so the code becomes simple, only need to start from the low, that is, factor starting from 1, each time multiplied by 10, until the current value, the n/factor=0, stop the operation.
The code is as follows:
#include <iostream> #include <stdio.h>using namespace std;int compute (int N) { int factor = 1; int low = 0; int high = 0; int now = 0; int cnt = 0; while (N/factor! = 0) {Now = (n/factor)%; High = N/FACTOR/10; Low = N-(n/factor) * factor; Switch (now) {case 0: cnt + = high * factor; break; Case 1: cnt + = high * factor + low + 1; break; Default: cnt + = (high + 1) * factor; } Factor *=; } return CNT;} int main () { int N; while (scanf ("%d", &n)! = EOF) { cout << Compute (N) << Endl; } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
1049. Counting Ones (30)