【问题描述】

请统计某个给定范围[L, R]的所有整数中,数字2出现的次数。

比如在给定范围[2, 22],数字2在数2中出现了1次,在数12中出现了1次,在数20中出现了1此,在数21中出现了1次,在数22中出现了2次,所以数字2在该范围内一共出现了6次。

Two
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
    int l,r,i,j,count;
    count=0;
    scanf("%d%d",&l,&r);
    for (i=l;i<=r;i++)
    {
        j=i;
        while (j>0)
        {
              if (j%10==2) count=count+1;
              j=j/10;
        }
    }
    printf("%d\n",count);
    return 0;
}       

 

 posted on 2013-03-30 22:11  Sky-Grey  阅读(244)  评论(0编辑  收藏  举报