The 11th Blue Bridge Cup-House number making

Problem description
Xiaolan wants to make house numbers for residents in a street.

There are a total of 2020 households on this street, and the house numbers are numbered from 1 to 2020.

Xiaolan’s method of making house plates is to first make numeric characters from 0 to 9, and finally paste the characters on the house plate as needed.

For example, house number 1017 needs to paste characters 1, 0, 1, 7 in sequence, that is, 1 character 0, 2 characters 1, and 1 character 7 are required.

How many characters 2 are required to make all the number 1 to 2020?

Answer submission
This is a question that fills in the blanks with the result. You only need to calculate the result and submit it.
The result of this question is an integer. Only fill in this integer when submitting the answer, and fill in the extra content will not be scored.


Answer: 624


answer:

#include <iostream>
using namespace std;

int ans;

void check(int n)
{
    
    
	while(n)
	{
    
    
		int t = n % 10;
		if(t == 2) ans ++;
		n /= 10;
	}
}

int main()
{
    
    
	for (int i = 1; i <= 2020; i ++)
		check(i);
	
	cout << ans << endl;
	return 0;		
}

Lanqiao Cup C/C++ Group Provincial Competition Past Years Questions

Guess you like

Origin blog.csdn.net/weixin_46239370/article/details/115327889