Wednesday, 19 February 2014


Number of  Digit in a Given Range



#include<iostream>
#include<stdio.h>
using namespace std;

int a[10][1000001];

int main(){
a[0][0]=1;
for(int i=1;i<=1000000;i++){
    int n=i;
    while(n){
    a[n%10][i]++;
    n=n/10;

    }

    a[0][i]+=a[0][i-1];
    a[1][i]+=a[1][i-1];
    a[2][i]+=a[2][i-1];
    a[3][i]+=a[3][i-1];
    a[4][i]+=a[4][i-1];
    a[5][i]+=a[5][i-1];
    a[6][i]+=a[6][i-1];
    a[7][i]+=a[7][i-1];
    a[8][i]+=a[8][i-1];
    a[9][i]+=a[9][i-1];
}

//uncomment this to see the table.
/*        
for(int j=0;j<10;j++){
for(int i=20;i<=40;i++)
cout<<a[j][i]<<"  ";
cout<<endl;
}
*/

int t;
cin>>t;          //no of times u wanna query
while(t--){
int r1,r2,num,ans;
cin>>r1>>r2>>num;                   //lower limit , upper limit ,  and number like  1 18 1
if(r1==0)
ans=a[num][r2];
else
ans=a[num][r2]-a[num][r1-1];
cout<<ans<<endl;

}



}

1 comment: