【問題描述】
小藍特别喜歡 2,今年是公元 2020 年,他特别高興。
他很好奇,在公元 1 年到公元 2020 年(包含)中,有多少個年份的數位中
包含數字 2?
代碼如下:
#include <iostream>
using namespace std;
int main() {
long long ans = 0;
for (int i = 1; i <= 2020; i++) {
int c = i;
while (c) {
if (c % 10 == 2) {
ans++;
break;
}
c = c / 10;
}
}
cout << ans << endl;
}