美文网首页
4-9 统计个位数字

4-9 统计个位数字

作者: Muzi_Jin | 来源:发表于2017-04-12 13:27 被阅读0次

    Attention: 如果喜欢我写的文章,欢迎来我的github主页给star
    Githubgithub.com/MuziJin

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

    函数接口定义

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

    其中ND都是用户传入的参数。N的值不超过int的范围;D是[0, 9]区间内的个位数。函数须返回N中D出现的次数。

    裁判测试程序样例

    #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;
    }
    
    /* 你的代码将被嵌在这里 */
    

    输入样例

    -21252 2
    

    输出样例

    3
    

    Code

    int Count_Digit ( const int N, const int D )
    {
        int temp;
        int n = abs(N);
        int d = 0;
        
        if(n == 0 && N == 0) //注意0的情况 
            d = 1;
        
        while( n>0 )
        {   
            temp = n%10;
            if( temp == D)
                d++;
            n /= 10;
        }
        return d;
    }
    

    转载请注明出处:github.com/MuziJin

    相关文章

      网友评论

          本文标题:4-9 统计个位数字

          本文链接:https://www.haomeiwen.com/subject/tvlmattx.html