1.5 41: Digital Statistics

Description
Please count the number of occurrences of the number 2 among all integers in a given range [L, R].

For example, given the range [2, 22], the number 2 appears once in the number 2, once in the number 12, once in the number 20, once in the number 21, and appears in the number 22 2 times, so the number 2 appears 6 times in this range.

Input
Input 1 line, two positive integers L and R, separated by a space.
Output There
is 1 line of output, indicating the number of occurrences of number 2.
Sample input
Sample #1:
2 22

Sample #2:
2 100
sample output
Sample #1:
6

Example #2:
20

#include <iostream>
using namespace std;
int main()
{
    
    
	int l,r,count=0,t, m;
	cin>>l>>r;
	for(int i=l;i<=r;i++)
	{
    
    
		m = i;
		while(m)
		{
    
    
			t=m%10;
			if(t==2)
			{
    
    
				count++;
			}				
			m /= 10;
		}
	}
	cout<<count<<endl;
	return 0;
}
l, r=map(int, input().split())
count = 0
for i in range(l, r+1):
    while i:
        if i%10 == 2:
            count += 1
        i //= 10
print(count)

Guess you like

Origin blog.csdn.net/yansuifeng1126/article/details/112957487
41