码迷,mamicode.com
首页 > 其他好文 > 详细

数字9

时间:2020-04-23 23:12:11      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:end   ring   数位   pre   string   include   for   main   sprint   

【问题描述】
在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 }

 

 

数字9

标签:end   ring   数位   pre   string   include   for   main   sprint   

原文地址:https://www.cnblogs.com/fate-/p/12764021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!