12678 - Count 1s
raw download clone
C
views 21
,
size 1138 b
#include <stdio.h>

int main(void)
{
    int t, i, a, b, j, k, d, x, p, y, q;

    scanf("%d", &t);
    for(i=1; i<=t; i++)
    {
        scanf("%d%d", &a, &b);
        x = 0;
        y = 0;
        d = 1;
        a = a - 1;
        for(j=0; j<=6; j++)
        {
            p = a / (10 * d);
            x += d * p;
            p = a % (10 * d);
            if(p / d == 0)
            {
                x += 0;
            }
            else if(p / d == 1)
            {
                x += p - d + 1;
            }
            else
            {
                x += d;
            }
            d *= 10;
        }
        d = 1;
        for(k=0; k<=6; k++)
        {
            q = b / (10 * d);
            y += d * q;
            q = b % (10 * d);
            if(q / d == 0)
            {
                y += 0;
            }
            else if(q / d == 1)
            {
                y += q - d + 1;
            }
            else
            {
                y += d;
            }
            d *= 10;
        }
        printf("%d\n", y - x);
    }

    return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.