码迷,mamicode.com
首页 > 其他好文 > 详细

数字分身术

时间:2019-02-24 20:11:24      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:scanf   ++   区间   scan   多少   循环   span   for   bsp   

试计算在区间 1 到 n 的所有整数中,数字 x(0 ≤ x ≤ 9)共出现了多少次?例如,在 1 到 11 中,即在 1、2、3、4、5、6、7、8、9、10、11 中,数字 1 出现了 4 次。

#include<stdio.h>
int main()
{
    int i,j,n,x,f=0;
    scanf("%d %d",&n,&x);
    for(i=1;i<=n;i++)//循环控制范围
       for(j=i;j;j/=10)//分级数剥离数字计数
          if(j%10==x)
            f++;
     printf("%d\n",f);
return 0;
}

 

数字分身术

标签:scanf   ++   区间   scan   多少   循环   span   for   bsp   

原文地址:https://www.cnblogs.com/ZZ34/p/10427591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!