思路:分别对每位1的出现次数进行统计
#include<iostream>
using namespace std;
int main() {
long long num, count = 0;
cin >> num;
long long digit = 1;
while (digit <= num) {
long long times = num / (digit * 10);
count += times * digit;
int remain = num % (digit * 10);
if (remain >= digit * 2) {
count += digit;
}
else if (remain >= digit) {
count += remain - digit + 1;
}
digit *= 10;
}
cout << count;
}
/*
digit = 1:
times = 132 / 10 = 13
count += 13 * 1: (1, 11, 21, ... , 121)
remain = 2 >= 2: count += 1 * 1: (131)
digit = 10:
times = 132 / 100 = 1
count += 1 * 10: (010, 011, ... , 019)
remain = 32 > 20: count += 1 * 10: (110, 111, ... , 119)
digit = 100
remain = 132 < 200: count += 132 - 100 + 1 = 33: (100, 101, ... , 132)
*/