Fork me on GitHub
文章目录
  1. 1. 题目来源
    1. 1.0.1. 问题描述
    2. 1.0.2. 算法设计
    3. 1.0.3. 数据输入
    4. 1.0.4. 结果输出
    5. 1.0.5. 输入文件示例
    6. 1.0.6. 输出文件示例
    7. 1.0.7. C++实现
  • 2. 感谢
  • 题目来源


    计算机算法设计与分析第二版-王晓东 P8

    问题描述


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

    算法设计


    给定表示书的总页码的十进制数n(1<=n<=10^9),计算书的全部页码中分别用到多少次数字0, 1, 2, …, 9.

    数据输入


    输入数据由文件名为input.txt的文本文件提供.每个文件只有一行,给出表示书的总页码的整数n.

    结果输出


    将计算结果输出到文件output.txt.输出文件共有10行,在第k行输出页码中用到数字k-1的次数,k = 1, 2, …, 10.

    输入文件示例


    1
    2
    input.txt
    11

    输出文件示例


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    output.txt
    1
    4
    1
    1
    1
    1
    1
    1
    1
    1

    C++实现


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    /*
    * @Author: Scott Wang
    * @Date: 2016-04-07 23:18:13
    * @Last Modified by: Scott Wang
    * @Last Modified time: 2016-04-10 21:38:01
    */
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include "stdlib.h"
    #include <string>

    using namespace std;

    int initNumber();
    void calculate(int number, int* result);
    void output(int* result, int count);

    int main(int argc, char const *argv[])
    {
    int number;
    int result[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    number = initNumber();

    calculate(number, result);

    int count = sizeof(result) / sizeof(result[0]);

    output(result, count);

    return 0;
    }

    /**
    * 从文件中获取数字
    * @return 书的页码
    */
    int initNumber()
    {
    int num;
    istringstream iss;
    string line;
    ifstream input("input.txt", ios::in);
    if (!input.is_open())
    {
    cerr << "Can't open input.txt" << endl;
    exit(-1);
    }

    getline(input, line);
    iss.str(line);
    iss >> num;
    iss.clear();

    input.close();

    return num;
    }

    /**
    * 计算页码
    * @param number 页码
    * @param result 存储结果的数组(全部页码分别用到0-9多少次)
    */
    void calculate(int number, int* result)
    {
    int i, j;

    // cout<<number<<endl;

    for (i = 0; i <= number; ++i)
    {
    j = i;
    while (j > 0)
    {
    result[j % 10]++;

    j = j / 10;
    }
    }
    }

    /**
    * 将计算结果输出
    * @param result 要输出的结果(result[10])
    * @param count 结果数组的大小
    */
    void output(int* result, int count)
    {
    ofstream output("output.txt", ios::out);
    for (int i = 0; i < count; ++i)
    {
    output << result[i] << endl;
    }

    output.close();
    }

    感谢

    感谢访问我的个人博客的朋友,如果您感觉本站对您搜索的问题有所帮助,并感觉对本站还满意的话,顶一下吧,希望您把本站分享给您的朋友!在此对您表示由衷的谢意! :)