首页 > 其他分享 > 1 的个数

1 的个数

时间:2022-08-22 12:11:19浏览次数:50  
标签:10 nums int pow 个数 res left

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
From: https://www.cnblogs.com/xjtfate/p/16612410.html

相关文章