2017年校招全国统一模拟笔试 页码统计

  • 一个数字n, 问从1到n中, 0-9总共出现了多少次?
  • 对于一个数字n, 若此时1-n中,0-9的个数分别在 ans 数组中存在, 那么对于 res = n10+9的这个数字,在1-res中,0-9的个数分别为 ans[i]10 + n + (i != 1), 0<=i<=9;
  1. 其中ans[i]*10, 是 res / 10 这一部分, 对于这一部分每一个数字后面都有一个0-9的对应。所以,res这一部分是res/10的10倍;
  2. 对于n,是res%10这一部分,即个位数的所得。 个位数的0-9, 每一个出现的次数, 都是前面部分的大小。
  3. 如果(i != 1),是因为,这是因为0-9中每个数字都还少一个没有被加上,再添上就可以了,对于0的这个,这部分是没有少一个的。因为这部分直到了9,并没有到后面的10。(每一部分都是从1-10, 不是从0-9, 因为初始位置为1不是0)

感觉自己理解了, 但是总觉得描述不太好。 可能是还不太熟练吧

#include<bits/stdc++.h>
using namespace std;

vector<int> solve(int n)
{
    vector<int> res(10, 0);
    if(n == 0)
        return res;

    if(n % 10 < 9)
    {
        res = solve(n-1);
        while(n)
        {
            res[n%10] ++;
            n /= 10;
        }
        return res;
    }

    res = solve(n/10);
    for(int i=0; i<res.size(); ++ i)
        res[i] = res[i] * 10 + n / 10 + (i>0);
    return res;
}

int main()
{
    int n;
    cin >> n;
    vector<int> ans = solve(n);
    for(int i=0; i<ans.size(); ++i)
    {
        if(i == 0)
            cout << ans[i] ;
        else
            cout << " " << ans[i];
    }
    cout << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/aiterator/p/6539348.html