这题的解决方法和保存1000!的方法类似,核心代码是j = i % 10,i /= 10;
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int Count_Digit(const int N, const int D); int main() { int N, D; scanf("%d %d", &N, &D); printf("%d ", Count_Digit(N, D)); return 0; } int Count_Digit(const int N, const int D) { int i = N > 0 ? N : -N; // 对N取绝对数 int j = 0, b = 0; // b用来统计出现D的次数,i/10= j,j用来保存结果 do { j = i % 10; if (j == D) b++; i /= 10; } while (i > 0); return b; }