Ordinary Number

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.

输入
第一行包含一个整数t (1≤t≤104).然后t测试案例如下。

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

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


输入
6
1
2
3
4
5
100
输出
1
2
3
4
5
18
题意就是求出1-n中 个位数相等的数字个数
我们可以想到
1-9 里面有9个
11-99 也有9个
111-999 也有9个
所以 如果这个数字n是四位数那么 答案为93+k
这个3就是数字位数减一 这个k就是当前数字位数里面满足条件的个数 因为我们已经知道他是四位数了 那么就拿1111
1~1111*9再跟n比较就行了:刚开始想的比较复杂,wa了好几次,难受

#include<cmath>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
#include<queue>
#include<map>
const int N=1e6+10;
#define  ll long long
int  a[20];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long n,m,sum=0,s;
        int  k=0,i,j;
        scanf("%lld",&n);
        m=n;
        while(m)
        {
            a[k++]=m%10;   //把每个数分解
            m/=10;
        }
        if(k==1)
        {
            s=n;
        }
        else
        {
            s=(k-1)*9;
            for(int i=0; i<k; i++)
                sum=sum*10+1;
        //      printf("@@@@%d\n",sum);
            for(int i=1; i<=9; i++)
            {
                if(sum*i<=n)
                    s++;
                else
                    break;
            }
        }
        printf("%lld\n",s);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值