zoukankan      html  css  js  c++  java
  • 1201-统计数字问题

    描述

     

    一本书的页码从自然数1 开始顺序编码直到自然数n。书的页码按照通常的习惯编排,

    每个页码都不含多余的前导数字0。例如,第6 页用数字6 表示,而不是06 或006 等。数字计数问题要求对给定书的总页码n,计算出书的全部页码中分别用到多少次数字0,1,2,…,9。

    给定表示书的总页码的10 进制整数n (1≤n≤109) 。编程计算书的全部页码中分别用到多少次数字0,1,2,…,9。

    输入

    输入只有1 行,给出表示书的总页码的整数n。

    输出

    输出共有10行,在第k行输出页码中用到数字k-1 的次数,k=1,2,…,10。

    样例输入

    11

    样例输出

    1

    4

    1

    1

    1

    1

    1

    1

    1

    1

    #include <iostream>
    #include <cmath>
    using namespace std;
    void statNumber(int n) {
        int m, i, j, t, x, len = log10(n);
        char d[16];
        int pow10[12] = {1}, count[10] = {0};
    
        for(i = 1; i < 12; i++) {
            pow10[i] = pow10[i-1] * 10;
        }
        sprintf(d, "%d", n);
        m = n+1;
        for(i = 0; i <= len; i++) {
            x = d[i] - '0';
            t = (m-1) / pow10[len-i]; 
            
            count[x] += m - t * pow10[len-i];         
            t /= 10;
            j = 0;
            while(j <= x-1) {
                count[j] += (t + 1) * pow10[len-i];
                j++;
            }
            while(j < 10) {
                count[j] += t * pow10[len - i];
                j++;
            }
            count[0] -= pow10[len-i]; 
        }
        for(j = 0; j < 10; j++) {
           printf("%d
    ", count[j]);
        }
    }
    int main() {
    int n;
    while(cin >> n) {
       statNumber(n);
    }
    return 0;
    }  
    

      

  • 相关阅读:
    基于antlr的表达式解析器
    ANTLR语法层的选项及动作
    Understanding ANTLR Grammar Files
    写给Git初学者的7个建议
    Top 8 Diagrams for Understanding Java
    技术面不深入
    一个初级程序员学习新技术的策略
    SoftReference,WeakReference&WeakHashMap
    探索Antlr(Antlr 3.0更新版)
    Five minute introduction to ANTLR 3
  • 原文地址:https://www.cnblogs.com/Rosanna/p/3437019.html
Copyright © 2011-2022 走看看