此代码是书的页码求解。但是我看不懂原理
输入n是书的页数,然后返回从1到n中1的出现次数#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
int sum = 0;
int k;
for(int i = 1; k = n / i;i *= 10)
{
sum += k/10 * i;
int tem = k % 10;
if(tem > 1)
sum += i;
if(tem == 1)
sum += n - k*i + 1;
}
printf("%d\n", sum);
return 0;
}