UVa1640 - The Counting Problem(数位统计)
来源:互联网 发布:网络主播怎么注册 编辑:程序博客网 时间:2024/06/05 10:05
题意:
统计两个整数a,b之间各个数字(0~9)出现的次数,如1024和1032,他们之间的数字有1024 1025 1026 1027 1028 1029 1030 1031 1032 总共有10个0,10个1,3个3等等。
分析:
因为前导0的干扰,为了计算方便暂时都先计算在内,之后再减;
如果是0~199,那么百位上的0和1各出现一次,s剩下的就是两个00~99,总共两百个二位数,而每个数出现的次数都一样,都是2*(99-00+1)/10;
那么任意的数都可以分解成类似的数字,如3426,则可以分成0000~2999,3000~3399,3400~3419,3420~3426几个部分各自计算,再求和按位减去前导0的个数。
#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;void Cal(char *s,int *num){ int i,j,k,n,t,m=atoi(s); n=strlen(s); for(i=k=1;i<n;i++) k*=10,num[0]-=k; for(i=0;i<n;i++,k/=10){ for(j=0;j<s[i]-'0';j++) num[j]+=k; for(t=0;t<10;t++) num[t]+=k/10*(n-i-1)*j; if(i+1<n) num[j]+=atoi(s+i+1); num[j]++; }}int main(){ char s[22]; int i,n,m,a[11]={0},b[11]={0}; for(;scanf("%d%d",&n,&m),m+n;){ if(n>m) swap(n,m); memset(a,0,sizeof(a)); sprintf(s,"%d",n-1); Cal(s,a); memset(b,0,sizeof(b)); sprintf(s,"%d",m); Cal(s,b); for(i=0;i<9;i++) printf("%d ",b[i]-a[i]); printf("%d\n",b[9]-a[9]); }}
0 0
- UVa1640 - The Counting Problem(数位统计)
- uva1640(数位统计)
- UVA1640 数位统计
- UVALive3261 UVA1640 POJ2282 HDU1663 ZOJ2392 The Counting Problem
- uva 1640 The Counting Problem (数位dp||统计0-9的个数)
- POJ2282:The Counting Problem(数位DP)
- The Counting Problem - POJ 2282 数位dp
- poj2282 The Counting Problem 数位dp
- POJ 2282 The Counting Problem (数位dp)
- poj 2282 The Counting Problem (数位DP)
- poj 2282 The Counting Problem 按位统计
- UVA 1640 The Counting Problem(统计题,找规律 + dfs)
- pku2282 The Counting Problem
- The Counting Problem poj2282
- poj2282 The Counting Problem
- POJ2282 The Counting Problem:
- WOJ1254-The Counting Problem
- The Counting Problem UVA
- xml 布局嵌套
- 页面没有绘制完成,获取控件大小
- android:layout_above="@id/xxx" --将控件置于给定ID控件之上android:layout_below="@id/xxx" -
- centos6.x修改网卡名称em1、2...为eth0、1...
- Hibernate中Session的get和load
- UVa1640 - The Counting Problem(数位统计)
- 构建NFS系统间文件共享
- Servlet的方法对象总结
- 程序员必知的8大排序
- iOS7.0之后tableview的cell分割线不靠边问题
- 自己写的一种字符串搜索
- Git操作——分支管理(一)
- 使用Builder模式进行美化代码
- 用分治法查找数组元素的最大值和最小值