其他分享
首页 > 其他分享> > (数位统计dp+GOOD)acwing 338. 计数问题

(数位统计dp+GOOD)acwing 338. 计数问题

作者:互联网

338. 计数问题

题目链接https://www.acwing.com/problem/content/340/
题目:
在这里插入图片描述
在这里插入图片描述


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int get_ct(int u){
    int ct=0;
    while(u){
        ct++;
        u/=10;
    }
    return ct;
}

int get(int u,int id){
    int ct=get_ct(u);
    int res=0;
    for(int j=1;j<=ct;j++){
        int k=pow(10,j-1);
        int r=u%k;
        int l=u/k/10;
        int temp=u/k%10;
        if(id) res+=l*k;
        if(!id&&l) res+=(l-1)*k;
        if(id==temp&&(id||l))
            res+=r+1;
        if(id<temp&&(id||l))
            res+=k;
    }
    return res;
}

int main(){
    int a,b;
    while(cin>>a>>b){
        if(!a&&!b) break;
        if(a>b) swap(a,b);
        for(int i=0;i<10;i++){
            printf("%d ",get(b,i)-get(a-1,i));
        }
        printf("\n");
    }
    return 0;
}


标签:GOOD,338,get,int,res,计数问题,include,id,ct
来源: https://blog.csdn.net/weixin_46028214/article/details/117336358