输入整数n,其长度为len
如果n长度为1时直接输出n+1
否则判断长度为len所有位次全是n最高位的整数tmp(如n==897就是tmp==888)
如果n<tmp
(map<int,int> ma存的时我们每个数字用的几次)
如果n>=tmp
最后遍历输出
#include"bits/stdc++.h"
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define endl '\n'
map<int, int> ma;
int calc(int len, int first)
{
//cout << len << ' ' << first << endl;
int ans = 0;
for (int i = 0; i < len; i++) ans = ans * 10 + first;
return ans;
}
signed main()
{
int n;
cin >> n;
int len = log10(n) + 1;
if (len == 1) return cout << n + 1 << endl, 0;
//cout << len << endl;
int first = to_string(n)[0] - '0';
int tmp = calc(len, first);
//cout << tmp << endl;
if (n < tmp)
{
for (int i = 1; i < first; i++) ma[i] = len;
for (int i = first; i <= 9; i++) ma[i] = len - 1;
ma[0] = len - 1;
}
else
{
for (int i = 1; i <= first; i++) ma[i] = len;
for (int i = first + 1; i <= 9; i++) ma[i] = len - 1;
ma[0] = len - 1;
}
int ans = 0;
for (auto item : ma) ans += item.second;
cout << ans;
}