AcWing
  • 首页
  • 题库
  • 题解
  • 分享
  • 问答
  • 活动
  • 应用
  • 吐槽
  • 登录/注册

AcWing 1533. 1 的个数

作者: 作者的头像   fanfande ,  2021-02-23 15:26:36 ,  阅读 1


0


#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>

using namespace std;

int cal(int n)
{
    vector<int>  nums;
    while(n) nums.push_back(n%10),n/=10;

    int res=0;
    for(int i=0;i<nums.size();i++)
    {
        int d=nums[i];
        int l=0,r=0,power=0;
        for(int j=nums.size()-1;j>i;j--) l=l*10+nums[j];
        for(int j=i-1;j>=0;j--)  r=r*10+nums[j],power++;

        if(d==0)  res+=l*pow(10,power);
        else if(d==1)  res+=(l*pow(10,power)+r+1);
        else  res+= (l+1)*pow(10,power);
    }
    return res;
}

int main()
{
    int a;
    cin>>a;
    cout<<cal(a)<<endl;
    return 0;

}

0 评论

你确定删除吗?

© 2018-2021 AcWing 版权所有  |  京ICP备17053197号-1
联系我们  |  常见问题
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息