算法设计与分析: 1-1 统计数字问题

1-1 统计数字问题


问题描述

一本书的页码从自然数1 开始顺序编码直到自然数n。书的页码按照通常的习惯编排, 每个页码都不含多余的前导数字0。例如,第6 页用数字6 表示,而不是06 或006 等。数 字计数问题要求对给定书的总页码n,计算出书的全部页码中分别用到多少次数字0,1, 2,…,9。
给定表示书的总页码的10 进制整数n (1≤n≤ 109 10 9 ) 。编程计算书的全部页码中分别用 到多少次数字0,1,2,…,9。


输入

每个文件只有1 行,给出表示书的总页码的整数n。


输出

程序运行结束时,输出文件共有10行,在第k行 输出页码中用到数字k-1 的次数,k=1,2,…,10。


Java: version-1

import java.io.*;

public class Main {

    public static void main(String[] args) {
        String input = "/Users/dijk/IdeaProjects/tongjishuziwenti/input.txt";
        String output = "/Users/dijk/IdeaProjects/tongjishuziwenti/output.txt";
        BufferedReader reader = null;
        BufferedWriter writer = null;
        int n = 0;
        int i,j,v;
        int[] count = new int[10];

        try{
            //Read page number n from input.txt file
            reader = new BufferedReader(new FileReader(input));
            writer = new BufferedWriter(new FileWriter(output));
            String number = null;
            number = reader.readLine();
            if(number != null){
                try{
                    n = Integer.parseInt(number);
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }
            }

            //Count number 0-9 existed times
            for (i=1; i<=n; i++){
                j=i;
                do{
                    v = j%10;
                    j = j/10;
                    count[v]++;
                }while(j>0);
            }

            //Write number 0-9 and its existed times to output.txt
            for(int k=1; k<=10; k++){
                System.out.println(k-1 +":" + count[k-1]);
                writer.write(k-1 +":" + count[k-1] + "\n");
            }

            reader.close();
            writer.close();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            if(reader != null){
                try{
                    reader.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(writer != null){
                try{
                    writer.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

Java: version-2

import java.io.*;

public class Main {

    public static void main(String[] args) {
        String input = "/Users/dijk/IdeaProjects/tongjishuziwenti/input.txt";
        String output = "/Users/dijk/IdeaProjects/tongjishuziwenti/output.txt";
        BufferedReader reader = null;
        BufferedWriter writer = null;
        int n = 0;
        int i,j;
        int[] count = new int[10];

        try{
            //Read page number n from input.txt file
            reader = new BufferedReader(new FileReader(input));
            writer = new BufferedWriter(new FileWriter(output));
            String number = null;
            number = reader.readLine();
            if(number != null){
                try{
                    n = Integer.parseInt(number);
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }
            }

            //Count number 0-9 existed times
            int length = (int) Math.log10(n);//length的值为总页码数值n的位数减去1
            int curDigit, higher, lower;
            for(i=0; i<=length; i++){
                //eg: 如果 n=12345, curDigit=3,则higher=12,lower=45
                //则在数字3这个位置:
                //1、大于3的数字(4-9)出现的次数与前面的数字(higher)和该数字所在的位置(百位)有关:12*100=1200
                //2、数字3出现的次数与前面的数字(higher)、后面的数字(lower)和该数字所在的位置(百位)有关:12*100+45+1=1246
                //3、小于3的数字(0、1、2)出现的次数与前面的数字(higher)和该数字所在的位置(百位)有关:(12+1)*100=1300
                curDigit = (n/(int)Math.pow(10,length-i))%10;
                higher = n/(int)Math.pow(10,length-i+1);
                lower = n%(int)Math.pow(10,length-i);

                //equal current digit
                count[curDigit] += lower+1;

                //smaller than current digit
                for(j=0; jint)Math.pow(10,length-i)*(higher+1);
                }

                //equal and bigger than current digit
                for(j=curDigit; j<10; j++){
                    count[j] += (int)Math.pow(10,length-i)*higher;
                }
            }

            //减去多算的0
            for(i=0; i<=length; i++){
                count[0] -= (int)Math.pow(10,i);
            }

            //Write number 0-9 and its existed times to output.txt
            for(int k=1; k<=10; k++){
                System.out.println(k-1 +":" + count[k-1]);
                writer.write(k-1 +":" + count[k-1] + "\n");
            }

            reader.close();
            writer.close();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            if(reader != null){
                try{
                    reader.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(writer != null){
                try{
                    writer.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

分析

由0,1,2,…,9组成的所有m位数。从m个0到m个9共有 10m 10 m 个m位数。在这 10m 10 m 个m位数中,包含数字的总个数是 m10m m ∗ 10 m ,其中0,1,2,…,9每个数字出现次数相同,则每个数字均为 m10m1 m ∗ 10 m − 1 次。
可以首先把最高位的数字单独处理,然后再处理剩下的m-1位,最后把那些多余的0全部去掉。

Java: version-3

import java.io.*;

public class Main {

    public static void main(String[] args) {
        String input = "/Users/dijk/IdeaProjects/tongjishuziwenti/input.txt";
        String output = "/Users/dijk/IdeaProjects/tongjishuziwenti/output.txt";
        BufferedReader reader = null;
        BufferedWriter writer = null;
        int n = 0;
        int i,j,k;
        int[] count = new int[10];

        try{
            //Read page number n from input.txt file
            reader = new BufferedReader(new FileReader(input));
            writer = new BufferedWriter(new FileWriter(output));
            String number = null;
            number = reader.readLine();
            if(number != null){
                try{
                    n = Integer.parseInt(number);
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }
            }

            //Count number 0-9 existed times
            int length = (int) Math.log10(n);//length的值为总页码数值n的位数减去1
            int tempNum = n;
            int highestDigit,restNum;
            for(i=0; i<=length; i++){
                highestDigit = tempNum/(int)Math.pow(10,length-i);//获得最高位上的数字
                restNum = tempNum-highestDigit*(int)Math.pow(10,length-i);//去掉最高位剩下的数值
                count[highestDigit] += restNum+1;

                for(j=0; jint)Math.pow(10,length-i);
                    for(k=0; k<10; k++){
                        count[k] += (length-i)*(int)Math.pow(10,length-i-1);
                    }
                }

                tempNum = restNum;
            }

            //减去多算的0
            for(i=0; i<=length; i++){
                count[0] -= (int)Math.pow(10,i);
            }

            //Write number 0-9 and its existed times to output.txt
            for(k=1; k<=10; k++){
                System.out.println(k-1 +":" + count[k-1]);
                writer.write(k-1 +":" + count[k-1] + "\n");
            }

            reader.close();
            writer.close();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            if(reader != null){
                try{
                    reader.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(writer != null){
                try{
                    writer.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

Sample input 1

11

Sample output 1

0:1
1:4
2:1
3:1
4:1
5:1
6:1
7:1
8:1
9:1

Sample input 2

123

Sample output 2

0:22
1:57
2:27
3:23
4:22
5:22
6:22
7:22
8:22
9:22

Sample input 3

12345

Sample output 3

0:4664
1:8121
2:5121
3:4721
4:4671
5:4665
6:4664
7:4664
8:4664
9:4664

Reference

王晓东《计算机算法设计与分析》(第3版)P6

你可能感兴趣的:(Algorithm,Java,计算机算法设计与分析,计算机算法设计与分析)