[NOIP2013 Popularity Group] Counting issues

Topic link

Problem description
Try to calculate how many times the number x (0 ≤ x ≤ 9) appears in all 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 appears 4 times.

The input format is
2 integers n, x, separated by a space.

The output format is
1 integer, representing the number of occurrences of x.

Input and output sample
input #1
11 1
output #1
4
Description/Reminder
For 100% data, 1≤ n ≤ 1,000,000, 0 ≤ x ≤ 9.

Code:

#include<stdio.h>
int main()
{
    
    
	int x, n, count = 0;
	scanf("%d%d", &n, &x);
	for(int i = 1; i <= n; i++)
	{
    
    
		int m = i;
		while(m > 0)
		{
    
    
			if(m % 10 == x) count++;
			m /= 10;
		}
	}
	printf("%d", count);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/113747911