Count the number 2 appears in a given range

Description questions
Please count the number of all integers a given range [L, R], the number 2 appears. For example a given range [2, 22], the number 2 appears in the number 2 of 1, 1 occurrence in number 12, appear more than once in the number of 20, the 1 occurrence number 21, appears in the number of 22 2, the number 2 appeared a total of six times in this range.
Input Format
An input common line, separated by a space between two positive integers L and R.
Output Format
The output common line 1, numeral 2 denotes the number appears.
Sample input
Sample # 1: 
222 

Sample # 2: 
2100
Sample Output
Sample # 1: 
6 

Sample # 2: 
20 is
#include <stdio.h>
int main()
{
    int l,r,i,k,y;
    while(scanf("%d%d",&l,&r)!=EOF)
    {
        k=0;
        for(i=l;i<=r;i++)
        {
            y=1;
            while(1)
            {
                if(i/y%10==2)
                    k++;
                y*=10;
                if(i/y==0)
                    break;
            }
        }
        printf("%d\n",k);
    }
    return 0;
}

Published 32 original articles · won praise 9 · views 70000 +

Guess you like

Origin blog.csdn.net/yi__cao/article/details/78502372