6-9 统计个位数字

题目

https://pintia.cn/problem-sets/14/problems/741

本题要求实现一个函数,可统计任一整数中某个位数出现的次数。例如-21252中,2出现了3次,则该函数应该返回3。

程序样例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int Count_Digit ( const int N, const int D );

int main()
{
int N, D;

scanf("%d %d", &N, &D);
printf("%d\n", Count_Digit(N, D));
return 0;
}

/* 你的代码将被嵌在这里 */

关键代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int Count_Digit ( const int N, const int D ){
int M = N,count = 0,i;
if(M<0){
M = -1*M;
}
if(M==0 && D==0){
count = 1;
}
while(M>0){
i = M%10;
if(i == D){
count++;
}
M = M/10;
}
return count;
}