zoukankan      html  css  js  c++  java
  • bzoj1833 [ZJOI2010]count 数字计数

    【bzoj1833】[ZJOI2010]count 数字计数

    Description

    给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次。

    Input

    输入文件中仅包含一行两个整数a、b,含义如上所述。

    Output

    输出文件中包含一行10个整数,分别表示0-9在[a,b]中出现了多少次。

    Sample Input

    1 99

    Sample Output

    9 20 20 20 20 20 20 20 20 20

    HINT

    30%的数据中,a<=b<=10^6;
    100%的数据中,a<=b<=10^12。

    这道题51nod 2级算法题(数据比这题要大),但是居然是省选题,无语了。这个很好推的。
     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cmath>
     5 #include<cstring>
     6 using namespace std;
     7 
     8 typedef long long LL;
     9 
    10 LL st,ed,ans[10]={0};
    11 
    12 LL make(LL num,int now)
    13 {
    14     LL res=0,tail=0,mi=1;
    15     if (num<10&&now==0) return 0;
    16     while (num!=0)
    17     {
    18         if (now==0&&num<10) break;
    19         int x=num%10;
    20         num/=10;
    21         res+=(num-(now==0))*mi;
    22         if (x>now) res+=mi;
    23         if (x==now) res+=tail+1;
    24         tail=(LL)x*mi+tail,mi*=10;        
    25     }
    26     return res;
    27 }
    28 int main()
    29 {
    30     scanf("%lld%lld",&st,&ed);
    31     for (int i=0;i<=9;i++)
    32     {
    33         ans[i]=make(ed,i)-make(st-1,i);
    34     }
    35     for(int i=0;i<=8;i++)
    36         cout<<ans[i]<<" ";
    37     cout<<ans[9];     
    38 }
  • 相关阅读:
    spring in action小结4.1
    spring in action小结3 运行时值注入
    python-__init__.py 与模块对象的关系
    Python-常用库扩展
    Qt-优化布局结构
    Python-文件修改器
    C语言-数据结构(一)
    Python-PyQt4学习笔记
    Python-PyQt4学习资料汇总
    Linux-查看C语言手册及man的特殊用法
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7444542.html
Copyright © 2011-2022 走看看