6-9 统计个位数字 (15 分)
本题要求实现一个函数,可统计任一整数中某个位数出现的次数。例如-21252中,2出现了3次,则该函数应该返回3。
函数接口定义:
int Count_Digit ( const int N, const int D );
其中N
和D
都是用户传入的参数。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
int Count_Digit ( const int N, const int D )
{
int a=N,b=0,count=0;
if(a<0)
a*=-1;
while(a>=10)
{
b=a%10;
a/=10;
if(b==D)
count++;
}
b=a%10;
if(b==D)
count++;
return count;
}
原文链接: https://www.cnblogs.com/lxzbky/p/10470237.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/400198
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!