动态规划--数位DP

https://www.acwing.com/problem/content/340/

 

 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 int get(vector<int> v,int l,int r){
 5     int res=0;
 6     for(int i=l;i>=r;i--){
 7         res=res*10+v[i];
 8     }
 9     return res;
10 }
11 int power10(int n){
12     int res=1;
13     for(int i=0;i<n;i++){
14         res*=10;
15     }
16     return res;
17 }
18 int count(int n,int x){
19     vector<int> v;
20     while(n){
21         v.push_back(n%10);
22         n/=10;
23     }
24     n=v.size();
25     int res=0;
26     for(int i=n-1-!x ;i>=0;i--){
27         if(i<n-1){
28             res+=get(v,n-1,i+1)*power10(i);
29             if(x==0){
30                 res-=power10(i);
31             }
32         }
33         if(v[i]==x){
34             res+=get(v,i-1,0)+1;
35         }else if(v[i]>x){
36             res+=power10(i);
37         }
38     }
39     return res;
40 }
41 int main(void){
42     int a,b;
43     while(cin>>a>>b,a||b){
44         if(a>b) swap(a,b);
45         for(int i=0;i<10;i++){
46             cout<<count(b,i)-count(a-1,i)<<" ";
47         }
48         cout<<endl;
49     }
50     return 0;
51 }

 

posted on 2021-03-13 17:05  greenofyu  阅读(33)  评论(0编辑  收藏  举报