[Cpp] // requirement: enter a positive integer n and calculate 1 ~ The number of times 1 appears between n # include <iostream> # include <math. h> using namespace std; void solve (); int input (); int power (int count); void main () {solve (); system ("pause ");} void solve () {int n, count = 0, I, sum = 0; n = input (); // count is a count to calculate the number of digits of n, for example, 1 is a single digit, and 2 is a hundred digits I = n; while (I) {I = I/10; count ++;} while (count --) {I = (n/power (count) % 10; // extracts the cout bit corresponding to count <I <endl; if (I = 1) {if (0 = count) // if the corresponding individual sum + = 1; else sum + = n % po Wer (count) + 1;} else if (I> 1) {sum + = power (count);} sum + = n/power (count + 1) * power (count);} cout <"One has" <sum <"1" <endl;} int input () {cout <"Enter the number you want to enter:" <endl; int n; cin> n; if (cin. fail () {cout <"your input is incorrect! "<Endl; exit (-1);} return n;} int power (int count) {int m = 1; if (count <0) {cout <"count value error! "<Endl; return-1;} else while (count --) m * = 10; return m;} I wrote the beautiful programming questions in my way and seldom read the source program, take a look at the train of thought at most. The complexity is O (n ).