Codeforces Round #719 (Div. 3)B. Ordinary Numbers(我打的不通畅的水题)

本文介绍如何使用编程解决一个数学问题,即给定一个整数n,求从1到n之间的所有普通数(所有位数相同的整数)的数量。通过一个C++程序实现并演示了计数方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Let’s call a positive integer 𝑛 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 𝑛, find the number of ordinary numbers among the
numbers from 1 to 𝑛.

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

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

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

Example inputCopy 6 1 2 3 4 5 100 outputCopy 1 2 3 4 5 18

#include<iostream>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int count=0;
        for(int i=1;i<=9;i++)
        {
            long long temp=i;
            while(n>=temp)
            {
                count++;
                temp=temp*10+i;
            }
        }
        printf("%d\n",count);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值