数字9

【问题描述】
在1至2019中,有多少个数的数位中包含数字9?
注意,有的数中的数位中包含多个9,这个数只算一次。例如,1999这个数包含数字9,在计算时只是算一个数。
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

 1 #include <iostream>
 2 
 3 using namespace std;
 4 const int N = 2019;
 5 
 6 bool check(int i) {
 7     char s[5];
 8     sprintf(s, "%d", i);
 9     string str(s);
10     return str.find('9') != string::npos;
11 }
12 
13 int main() {
14     int ans = 0;
15     for (int i = 9; i <= N; ++i) {
16         if (check(i)){
17             ans++;
18             cout<<ans<<":"<<i<<endl;
19         }
20     }
21     cout << ans << endl;
22     return 0;
23 }
 
原文地址:https://www.cnblogs.com/fate-/p/12764021.html