(PAT)实现一个统计整数中指定数字的个数的简单函数

#include <stdio.h>

int CountDigit( int number, int digit );

int main()
{
    
    
    int number, digit;

    scanf("%d %d", &number, &digit);
    printf("Number of digit %d in %d: %d\n", digit, number, CountDigit(number, digit));

    return 0;
}

int CountDigit( int number, int digit )
{
    
    
    int count = 0;
    if(number < 0)number = -number;
        
    while(number >= 10)
    {
    
    
        if( number % 10 == digit ) count++;
        number = number / 10;
    }
    if( number % 10 == digit )count++; 
    return count;
}

猜你喜欢

转载自blog.csdn.net/qq_52001969/article/details/112062832