How many digits appear in all integers from 0 to 100?

How many digits appear in all integers from 0 to 100?

Integers where 9 will appear in 0-100 are: 9, 19, 29, 39, 49, 59, 69, 79, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 , There are a total of 20 9s. A simple logarithmic analysis shows that the numbers can be divided into two categories:

  • Take the remainder with 10 to get 9: 9, 19, 29, 39, 49, 59, 69, 79, 89, 99.
  • Divide 10 to get 9: 90, 91, 92, 93, 94, 95, 96, 97, 98, 99. So we only need to record the number of occurrences of the two types of numbers.
  • Below is the code implementation
#include<stdio.h>
int main()
{
    
    
 int i=0;
 int n = 0,m=0;
 while (i <= 100)
 {
    
    
  if (i % 10 == 9)
  {
    
    
   ++m;
  }
  if (i / 10 == 9)
  {
    
    
   ++n;
  }
  ++i;

 }
 printf("%d\n", m + n);
 return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45796387/article/details/110452663