统计一本N页的书含有的数字各几个

来源:互联网 发布:制作app的软件 编辑:程序博客网 时间:2024/04/27 19:00
public class PageNum {    public static void main(String[] args){        int num = 1234<span style="white-space:pre"></span>;        int higher,len,temp,rest;        len = (int)Math.log10(num);        int[] array= new int[10];        for(int x:array){            x = 0;        }                temp = num;        for(int i=0;i<=len;i++) {            higher = (int) (temp / Math.pow(10, len - i)); //取得最高位            rest = temp - higher * (int) Math.pow(10, len - i);            array[higher] += rest + 1;            for (int j = 0; j < higher; j++) {                array[j] += Math.pow(10, len - i);                for (int k = 0; k < 10; k++) {                    array[k] += (len - i) * Math.pow(10, len - i - 1);                }            }            temp = rest;        }        for(int i=0;i<=len;i++) {            array[0] -= Math.pow(10, i);        }        for(int i:array){            System.out.print(i+" ");        }    }}

0 0
原创粉丝点击