1 的个数
作者:互联网
https://www.acwing.com/problem/content/1535/
`
思路:
暴力肯定是不可能的,枚举每一个的情况,分类讨论。
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
int n;
LL calc(int n){
vector<int> nums;
while (n){
nums.push_back(n % 10);
n /= 10;
}
LL res = 0;
for(int i = nums.size() - 1; i >= 0; i--){
int left = 0, right = 0, pow = 1; // 预处理abc efg pow10(i)
for(int j = nums.size() - 1; j > i; j--) left = left * 10 + nums[j];
for(int j = i - 1; j >= 0; j--){
right = right * 10 + nums[j];
pow = pow * 10;
}
int d = nums[i];
if (d < 1) res += left * pow;
else if (d == 1) res += left * pow + right + 1;
else res += (left + 1) * pow;
}
return res;
}
int main(){
scanf("%d", &n);
printf("%lld", calc(n));
return 0;
}
标签:10,nums,int,pow,个数,res,left 来源: https://www.cnblogs.com/xjtfate/p/16612410.html