UVa1640 统计问题

给出整数 a 、 b ,统计 a 和 b (包含 a 和 b )之间的整数中,数字 0,1,2,3,4,5,6,7,8,9 分别出现了多少次。 1 ≤ a,b ≤ 1 0 8 10^8 108 。注意, a 有可能大于 b 。

样例:

Sample Input
1 10
44 497
0 0
Sample Output
1 2 1 1 1 1 1 1 1 1
85 185 185 185 190 96 96 96 95 93

f d ( n ) f_d(n) fd(n) 表示 0~n-1 中数字 d 出现的次数,则所求的就是 f d ( b + 1 ) − f d ( a ) f_d(b+1)-f_d(a) fd(b+1)fd(a) 。例如,要统计 0~4567 中 4 的个数,可以分成几个区间。“模板”是指整数的集合。

范围模板集备注
0~9**指 0~9 的任意数字
10~99n*n 值 1~9 的任意数字
100~999n**
1000~39991***,2***,3***
4000~449940**,41**,…,44**
4500~4559450*,451*,…,455*
4560~45674560,4561,…,4567

对于 1**.. (假设后面的 * 有 k 个),统计其 4 的个数,因为总的数字有 1 0 k 10^k 10k 个(从 100…到 199…),一共有 k × 1 0 k k\times10^k k×10k* ,如果只考虑 * 所代表的数字,这些数字里面 0~9 出现的次数是均等的。所以数字 4 出现的次数为 k × 1 0 k − 1 k\times10^{k-1} k×10k1 。而 n**.. 里面数字 n(n!=0)的个数为 9 k × 1 0 k − 1 + 1 0 k 9k\times10^{k-1}+10^{k} 9k×10k1+10k

因为 int 类型占 4 个字节,所及计数的总数目为 2 32 > 4 ∗ 1 0 9 2^{32} > 4*10^9 232>4109,因为题目中最大值是 10 的 8 次方,我们可以用 int 存下。

完整程序:

//#define LOCAL
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <algorithm>
#include <cstring>
//#include <string>
#include <math.h>
#include <vector>
#include <map>
//#include <bitset>
#include <sstream>
//#include <map>

using namespace std;

typedef long long LL;

int pow10[10]; //计算以及存储 10^i 的值
int cnt[10];   // 计算以及存储 i*10^i 的值

int f(int d, int n)
{
    char s[10];
    sprintf(s, "%d", n);
    int len = strlen(s);
    int ans = 0;

    for (int i = 1; i < len; i++)
    { // *~n**..中 d 的个数
        if (i == 1)
            ans++;
        else
        {
            ans += 9 * cnt[i - 1]; // n**里面的*中 d 的个数
            if (d)
                ans += pow10[i - 1]; //n**中的 n 中 d 的个数
        }
    }

    int pre[10]; // 记录字符串 s[0~i] 有多少 d
    for (int i = 0; i < len; i++)
    {
        pre[i] = (s[i] - '0' == d ? 1 : 0);
        if (i > 0)
            pre[i] += pre[i - 1];
    }

    for (int i = 0; i < len; i++)
    {
        int maxd = s[i] - '0' - 1;
        int mind = 0;
        if (i == 0 && len > 1)
            mind = 1; //不能出现 012 这样的情况,但可以出现 0
        for (int j = mind; j <= maxd; j++)
        { //j 分别取 mind 到 maxd 这些数字
            ans += cnt[len - i - 1];
            if (i > 0)
            {
                ans += pre[i - 1] * pow10[len - i - 1];
            }

            if (j == d)
            {
                ans += pow10[len - i - 1];
            }
        }
    }
    return ans;
}

int main()
{
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif // LOCAL
    pow10[0] = 1;
    cnt[0] = 0;
    for (int i = 1; i < 10; i++)
    {
        pow10[i] = pow10[i - 1] * 10;
        cnt[i] = i * pow10[i - 1];
    }

    int a, b;
    while (cin >> a >> b && a && b)
    { // 题目说明, ab 都不能为 为0
        if (a > b)
            swap(a, b);
        for (int d = 0; d <= 9; d++)
        {
            if (d)
                printf(" ");
            cout << f(d, b + 1) - f(d, a);
        }
        cout << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值