1009 数字1的数量

思路:当初做的时候YY了好久一直YY不出来,查了题解发现好多种解法,学了一种最简单的,确实是一道不错的题,以下截图自其它大牛博客。

 1 #include <iostream>
 2 #include <queue>
 3 #include <stack>
 4 #include <cstdio>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <bitset>
 9 #include <algorithm>
10 #include <cmath>
11 #include <cstring>
12 #include <cstdlib>
13 #include <string>
14 #include <sstream>
15 #define x first
16 #define y second
17 #define pb push_back
18 #define mp make_pair
19 #define lson l,m,rt*2
20 #define rson m+1,r,rt*2+1
21 #define mt(A,B) memset(A,B,sizeof(A))
22 #define mod 1000000007
23 using namespace std;
24 typedef long long LL;
25 const int N=200000+10;
26 const LL INF=0x3f3f3f3f3f3f3f3fLL;
27 int ans=0;
28 int f(int n)
29 {
30     if(n<1)return 0;
31     else if(n>=1&&n<=9)return 1;
32     else
33     {
34         int bit=0,highbit=n;
35         while(highbit>=10)
36         {
37             highbit/=10;
38             bit++;
39         }
40         int p=pow(10,bit);
41         if(highbit==1)
42         {
43             return f(p-1)+f(n-p)+n-p+1;
44         }
45         else
46         {
47             return highbit*f(p-1)+f(n-highbit*p)+p;
48         }
49     }
50 }
51 int main()
52 {
53 #ifdef Local
54     freopen("data.txt","r",stdin);
55 #endif
56     int n;
57     cin>>n;
58     cout<<f(n)<<endl;
59 }
View Code

 

posted @ 2017-01-09 13:01  Kcl886  阅读(231)  评论(0编辑  收藏  举报