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就是当前数字位数里面满足条件的个数 因为我们已经知道他是四位数了 那么就拿11111~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;
}