AcWing 1533. 1 的个数
原题链接
中等
作者:
wjie
,
2020-07-16 16:07:51
,
所有人可见
,
阅读 795
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int exp(int a, int b)
{
int res = 1;
while (b)
{
if (b & 1) res *= a;
a *= a;
b >>= 1;
}
return res;
}
int get(vector<int> &nums, int l, int r)
{
int res = 0;
for (int i = l; i >= r; --i) res = res * 10 + nums[i];
return res;
}
int main()
{
int n;
scanf("%d", &n);
vector<int> nums;
while (n) nums.push_back(n%10), n /= 10;
int res = 0;
for (int i = nums.size()-1; i >= 0; --i)
{
if (i < nums.size()-1) res += get(nums, nums.size()-1, i+1) * exp(10, i);
if (nums[i] > 1) res += exp(10, i);
else if (nums[i] == 1) res += get(nums, i-1, 0) + 1;
}
printf("%d", res);
return 0;
}