标签:
问题描述:
一本书的页码从自然数1开始顺序编码直到自然数n。书的页码按照通常的习惯编排,每个页码都不含多余的前导数字0。例如第6页用6表示而不是06或006。
数字统计问题要求对给定书的总页码,计算出书的全部页码中分别用到多少次数字0,1,2,3,.....9。
算法分析:
下面是自己的实现代码:
package abc;
public class CountNum{
//递归求n位数中数字出现的次数
public static int count(int n){
if(n==1)
return 1;
else if(n<1)
return 0;
else
return 10*count(n-1)+(int)java.lang.Math.pow(10,n-1);
}
public static int[] judge(int m){
int[] num = new int[10];
String str = m+"";
int len = str.length();
int x = m;
int y;
int cs = (int)java.lang.Math.pow(10,len-1);
//从高位到低位
for(int i=len-1;i>0;i--){
y = x/cs;
for(int j=0;j<10;j++){
if(j<y)
num[j] += y*count(i)+cs;
else
num[j] += y*count(i);
}
//去除多余的0的个数
num[0] -= cs;
x = x%cs;
num[y] += x+1;
cs = cs / 10;
}
//最后到个位,每个数出现的次数
for(int i=0;i<=x;i++){
num[i]++;
}
return num;
}
public static void main(String[] args){
//System.out.print(count(9));
int[] s = judge(22);
for(int i=0;i<s.length;i++)
System.out.println(s[i]);
}
}
标签:
原文地址:http://www.cnblogs.com/disneyland/p/4491554.html