Number of 1
Mean:
Enter n to calculate the number of integers smaller than 10 ^ N containing 1.
Analyze:
This is a combination of post-thinking questions in mathematics.
Basic Idea: Combination of mathematical multiplication principle + rejection Principle
In the n-digit number, each item can be {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. Therefore, there are 10 ^ N types. To remove the case where each item is 0, you need to subtract one.
Where 1 is not selected for each user: {0, 2, 3, 4, 5, 6, 7, 8, 9}. Therefore, in a total of 9 ^ N, it is also necessary to remove all 0.
Time Complexity:O (N)
Source code:
// Memory time // 1347 K 0 Ms //: snarl_jsb # include <algorithm> # include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <Stack> # include <map >#include <string ># include <climits> # include <cmath> # define n 1000010 # define ll long longusing namespace STD; // enter n to calculate the number of 1 in the positive integer smaller than 10 ^ N; int main () {// freopen ("C: \ Users \ Asus \ Desktop \ cin.txt "," r ", stdin); // freopen (" C: \ Users \ Asus \ Desktop \ cout.txt "," W ", stdout); int N; while (CIN> N) {// If (n = 1) // puts ("1"); int Sol = 1; for (INT I = 1; I <= N; I ++) {Sol * = 10;} Sol --; int res = 1; for (INT I = 1; I <= N; I ++) {res * = 9 ;} res --; cout <sol-res <Endl;} return 0 ;}
Number Theory-combined mathematics-number of 1