题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
小蓝要为一条街的住户制作门牌号。
这条街一共有 2020 位住户,门牌号从 1 到 2020 编号。
小蓝制作门牌的方法是先制作 0 到 9 这几个数字字符,最后根据需要将字符粘贴到门牌上,例如门牌 1017 需要依次粘贴字符 1、0、1、7即需要 1 个字符 0,2个字符 1,1 个字符 7。
请问要制作所有的 1 到 2020 号门牌,总共需要多少个字符 2?
#include<iostream>
using namespace std;int cnt;int main()
{for(int i=1; i<=2020; ++i){int temp=i;while(temp){if(temp%10==2){cnt++; }temp /= 10;}}cout<<cnt;return 0;
}