PAT甲1049 Counting Ones (30)(30 分)

#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;

int a;

int main()
{
    scanf("%d",&a);
    int left=a,right=0,exp=1;
    int num=0;
    while(left>0)
    {
        int pop=left%10;
        left=left/10;
        if(pop>=2)
        {
            num+=(left+1)*exp;
        }
        else if(pop==0)
        {
            num+=left*exp;
        }
        else if(pop==1)
        {
            num+=left*exp+right+1;
        }
        right+=pop*exp;
        exp*=10;
    }
    printf("%d\n",num);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yhy489275918/article/details/81813494