Daily practice Blue Bridge Cup C/C++ Group B ~ House Number Production

Daily practice Blue Bridge Cup C/C++ Group B ~ House Number Production

topic:

Xiaolan wants to make house numbers for the residents of a street.
This street has a total of 2020 residents, with house numbers from 1 to 2020.
The method of Xiaolan to make the house number is to first make the number characters from 0 to 9, and finally paste the characters on the house number as needed. For example, the house number 1017 needs to paste the characters 1, 0, 1, and 7 in sequence, that is, one character 0 is required. 2 characters 1, 1 character 7.
How many characters 2 are needed in total to make all the number 1 to 2020?

Ideas : I wrote the ideas in the code comments. This is a fill-in-the-blank question, and you can directly output the answer after calculating it.

#include <stdio.h>
#include <stdlib.h>
/**
*小蓝要为一条街的住户制作门牌号。

*这条街一共有 20202020 位住户,门牌号从 11 到 20202020 编号。

*小蓝制作门牌的方法是先制作 00 到 99 这几个数字字符,最后根据需要将字符粘贴到门牌上,

*例如门牌 1017 需要依次粘贴字符 1、0、1、71、0、1、7,即需要 11 个字符 00,22 个字符 11,11 个字符 77。

*请问要制作所有的 11 到 20202020 号门牌,总共需要多少个字符 22?
*/ 
int main() {
    
    
	int num=0;//定义2的数量 
	int j,i;
	for( i=1;i<= 2020;i++)
	{
    
    
		j=i;
		while(j){
    
    
			if(j%10==2){
    
     //如果2%10等于2,num加1 
				num++;
			}
			j /=10;  //20/10等于2,然后2等于j,代入循环,2%10等于2,又num加1。 
			

		}
	} 
	printf("%d",num);
	return 0;

}

Answer:624

Guess you like

Origin blog.csdn.net/A6_107/article/details/122993872