[Compound] Count the number of digits 0,1,2,3,…,9 in the range from 1 & gt; n | MDIGITS
Threads: http://vn.spoj.com/problems/MDIGITS/
Given two integers a, b. Write all the numbers between a, b; including 2 This number.
Work out each digit 0, 1, .., 9 each number appearing many times.
Example, if a = 1024 and b = 1032, range will be
1024 1025 1026 1027 1028 1029 1030 1031 1032
and 10 number 0, 10 number 1, 7 number 2, …
I count the digits from 0->the, 0->b then take turns digits of each number (0->9) of b minus a.
League history at some 31627 (compared to the number 0), I put the following numbers respectively:

Consider the number in the first column, the number 0,1,2 10 ^ 4 numbers, number 3 will have 1627+1 = 1628 number (in the image above freezing of 0 not out, we folded down to the last number 00000)
Found from the column 2 onwards (Figure) It can be divided into clusters (3 cluster) where each cluster consists of chocolate 0->9 are 4 * 10 ^ 3 number.
So now we only consider the same for some 1627 only !
Once the count is completed, attention minus the number 0 Invalid (001) then 2 number 0 Invalid at the top.
#include <stdio.h>
#include <math.h>
int A[] = {1,10,100,1000,10000,100000,1000000,10000000,100000000};
void pro(int num, int len, int count[])
{
int n, i;
n = num / A[len];
if (num ==0)
{
count[n] += len+1;
return;
}
for(i=0;i<n;i++)
count[i] += A[len];
count[n] += num % A[len] + 1;
if(len==0) return;
for(i=0;i<10;i++)
count[i] += n*len*A[len-1];
pro(num % A[len], len-1, count);
}
int main()
{
int p1, p2, a, b, c, i;
while(scanf("%d%d",&a,&b)==2 && a+b)
{
if(a>b)
{
c = a;
a = b;
b = c;
}
a--;
int count1[10] = {1};
int count2[10] = {1};
if(a)
{
p1 = (int)floor(log10(a));
pro(a, p1, count1);
for(i=0; i<=p1; i++);
count1[0] -= A[i];
}
p2 = (int)floor(log10(b));
pro(b, p2, count2);
for(i=0; i<=p2; i++)
count2[0] -= A[i];
printf("%d",count2[0]-count1[0]);
for(i=1; i<10; i++)
printf(" %d",count2[i]-count1[i]);
printf("n");
}
return 0;
}



Recent Comments