题目
题解
整体思路就是判断每一位上对1的贡献个数。
对于一个数abcdef
,假设我们看c
所在位对1的贡献。
当c=0
时,贡献为ab * 1000
;
当c=1
时,贡献为ab * 1000 + (def+1)
当c>1
时,贡献为ab * 1000 + 1000
1000是因为c所在位的权重为1000。
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL ans;
int n, x, a[100], cnt;
int main()
{
cin >> n;
int x = n;
while (x) {
a[cnt ++] = x % 10;
x /= 10;
}
for (int i = 0, base = 1;i < cnt;i ++, base *= 10) {
int before = n / base / 10;
int after = n % base;
LL sum = before * base;
if (a[i] == 1) sum += after + 1;
else if (a[i] > 1) sum += base;
ans += sum;
}
cout << ans << endl;
return 0;
}