CodeForces - 1520B Ordinary Numbers

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 1520B Ordinary Numbers相关的知识,希望对你有一定的参考价值。

B. Ordinary Numbers
time limit per test: 2 seconds
memory limit per test: 256 megabytes

Let’s call a positive integer n ordinary if in the decimal notation all its digits are the same. For example, 1, 2 and 99 are ordinary numbers, but 719 and 2021 are not ordinary numbers.

For a given number n, find the number of ordinary numbers among the numbers from 1 to n.

Input
The first line contains one integer t (1≤t≤104). Then t test cases follow.

Each test case is characterized by one integer n (1≤n≤109).

Output
For each test case output the number of ordinary numbers among numbers from 1 to n.

Example
input
6
1
2
3
4
5
100
output
1
2
3
4
5
18

问题链接 CodeForces - 1520B Ordinary Numbers
问题简述:(略)
问题分析:(略)
AC的C++语言程序如下:

/* CodeForces - 1520B Ordinary Numbers */

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t, n;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);

        int k = n, w = -1, v = 1;
        while (k) {
            k /= 10;
            if (++w) {v *= 10; v++;}
        }

        printf("%d\\n", w * 9 + n / v);
    }

    return 0;
}

AC的C++语言程序如下:

/* CodeForces - 1520B Ordinary Numbers */

#include <bits/stdc++.h>

using namespace std;

int main()
{
    vector<int> v;
    for  (int i = 1; i <= 9; i++) {
        int p = 0;
        for (int j = 1; j <= 9; j++) {
            p = p * 10 + i;
            v.push_back(p);
        }
    }
    sort(v.begin(), v.end());

    int t, n;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        printf("%d\\n", upper_bound(v.begin(), v.end(), n) - v.begin());
    }

    return 0;
}

以上是关于CodeForces - 1520B Ordinary Numbers的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法根据地址生成位置坐标?

如何遍历 JSON 坐标并将注释构建为一个函数?

PE 导出目录表的 OrdinalBase 字段被忽略?

有了測试工具,傻瓜仍是傻瓜

带有遗留数据库的 Django 模型 utf8

codeforces上怎么看测试数据