Let's come to the simple question today Counting problem T1
topic description
Try to count how many times the number x (0≤x≤9) appears in all the integers in the interval 1 to n? For example, in 1 to 11, that is, in 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, the number 1 occurs 4 times.
enter
Input a total of 1 line, including 2 integers n and x, separated by a space.
output
Output a total of 1 line, containing an integer, indicating the number of occurrences of x.
sample input
11 1
sample output
4
hint
For 100% of the data, 1≤n≤1,000,000, 0≤x≤9
All I can say is, it's easy
This can be regarded as a water problem of NOIP, it is the first time I see that the problem of NOIP is so simple
Talk about ideas:
Traverse the loop, traverse from 1 to n, and then store a temporary value tmp similar to digital division. If tmp%10 == x, that is to say, one bit in tmp is x, store a variable sum, sum++, and finally Just output the sum, it's very simple
upper code
#include <stdio.h>
int main()
{
int n,x,sum=0;
scanf("%d %d",&n,&x);
for(int i=1;i<=n;i++)
{
int tmp=i;
while(tmp>0){
if(tmp%10==x) sum++;
tmp/=10;
}
}
printf("%d",sum);
return 0;
}