• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

李必恒

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

统计个位数字

本题要求实现一个函数,可统计任一整数中某个位数出现的次数。例如-21252中,2出现了3次,则该函数应该返回3.其中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;

}

int Count_Digit(const int N, const int D)
{
int count,temp,x;
x=N;
if(x<0)
x=-x;
do{
temp=x%10;
if(temp==D)
count++;
x=x/10;
}while(x>0);

return count;
}

 

posted on 2021-07-13 17:32  李必恒  阅读(248)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3