1049 Counting Ones(30 分)

分离一个数的左右数

#include
#include
using namespace std;
int ans;
int main()
{
    int n;
    scanf("%d", &n);
    int a = 1, left, right;
    while (n / a)
    {
        int now = n / a % 10;
        left = n / (a * 10);
        right = n%a;
        if (now == 0)ans += left*a;
        else if (now == 1)ans += left*a + 1 + right;
        else if (now > 1)ans += (left + 1)*a;
        a *= 10;
    }
    printf("%d", ans);
    return 0;
}

你可能感兴趣的:(1049 Counting Ones(30 分))