[Statement: This article is only intended for self-Summary and mutual communication, and may be omitted. Email: Mr_chenping@163.com]
Question:
Enter an integer n to calculate the number of times 1 appears and
Question Analysis:
I. In fact, the problem can be converted to the number of occurrences of 1 in a number, which involves basic remainder and Division operations.
Algorithm Implementation:
# Include
/*** Calculate the number of digits @ digit contained in the integer @ m */int count_digit_num (int m, int digit) {int count = 0; while (m) {if (m % 10 = digit) count ++; m = m/10;} return count;} int count_digit_one (int m) {int I = 0; int count = 0; for (; I <= m; I ++) {count + = count_digit_num (I, 1) ;}return count ;} int main (int argc, char * argv []) {int m = atoi (argv [1]); printf ("% d -----> % d \ n", m, count_digit_one (m ));}