计数问题

计数问题

![在这里插入图片描述]( https://img-blog.csdnimg.cn/b1396d9e59204808abb1e201d38b937f.png?x-oss-process=image/watermark ,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAUGFuc2XCtw,size_20,color_FFFFFF,t_70,g_se,x_16)
思路:
![在这里插入图片描述]( https://img-blog.csdnimg.cn/845ab83139264a908438425de9184b40.png?x-oss-process=image/watermark ,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAUGFuc2XCtw
,size_20,color_FFFFFF,t_70,g_se,x_16)
样例输入:

1 10
44 497
346 542
1199 1748
1496 1403
1004 503
1714 190
1317 854
1976 494
1001 1960
0 0

样例输出:

1 2 1 1 1 1 1 1 1 1
85 185 185 185 190 96 96 96 95 93
40 40 40 93 136 82 40 40 40 40
115 666 215 215 214 205 205 154 105 106
16 113 19 20 114 20 20 19 19 16
107 105 100 101 101 197 200 200 200 200
413 1133 503 503 503 502 502 417 402 412
196 512 186 104 87 93 97 97 142 196
398 1375 398 398 405 499 499 495 488 471
294 1256 296 296 296 296 287 286 286 247

代码模板:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 1e9;
int a,b;
vector<int> num;
int get(int l,int r){
	int res=0;
	for(int i=l;i<=r;i++)
	{
		res=res*10+num[i];
	}
	return res;
}

int power(int l,int r){
	int res=1;
	for(int i=l;i<=r;i++)
	{
		res=res*10;
	}
	return res;
}

int count(int n,int x){
	//导出
	num.erase(num.begin(),num.end()); 
	while(n){
		num.push_back(n%10);
		n/=10; 
	}
	reverse(num.begin(),num.end());
	
	n=num.size()-1;
	int res=0;
	for(int i=0;i<=n;i++)
	{
		
		if(i<=n){
			res+=(get(0,i-1)*power(i+1,n));
			if(!x) res-=power(i+1,n); //首位不能为 0  
		}
		
		if(num[i]==x){
			res+=(get(i+1,n)+1);
		}else if(num[i]>x){
			res+=power(i+1,n);
	}
	return res;
}


int main(){
	while(scanf("%d %d",&a,&b),a||b){
		if(a>b) swap(a,b);
		
		for(int i=0;i<10;i++)
			cout<<count(b,i)-count(a-1,i)<<" ";
		cout<<endl;
	}
	
	return 0;
}

 

posted @ 2022-03-22 14:07  panse·  阅读(47)  评论(0)    收藏  举报