Numbers containing 0's from 1 to N
PROBLEM :
Efficiently count how many integers from 1 to N contains 0's as a digit.
Input:
First line of the input contains the number of test cases T. Each line of test case contains the integer N.
Output:
Number of integers that contain 0 as a digit are printed.
Constraints:
1 <=T<= 100
1 <=N<= 10000
Example:
Input:
3
100
987
20
Output:
10
179
2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int count_zeroes(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<count_zeroes(no)<<endl ;
}
return 0;
}
int count_zeroes(int no)
{
int i,count ;
count=0 ;
for(i=1;i<=no;i++)
{
if((i>1000)&&((i/10)%10==0||(i/100)%10==0))
count++;
else if(i>10&&(i/10)%10==0)
count++ ;
else if(i%10==0)
count++ ;
}
return count ;
}
---------------------------------------------------------------------------------
Efficiently count how many integers from 1 to N contains 0's as a digit.
Input:
First line of the input contains the number of test cases T. Each line of test case contains the integer N.
Output:
Number of integers that contain 0 as a digit are printed.
Constraints:
1 <=T<= 100
1 <=N<= 10000
Example:
Input:
3
100
987
20
Output:
10
179
2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int count_zeroes(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<count_zeroes(no)<<endl ;
}
return 0;
}
int count_zeroes(int no)
{
int i,count ;
count=0 ;
for(i=1;i<=no;i++)
{
if((i>1000)&&((i/10)%10==0||(i/100)%10==0))
count++;
else if(i>10&&(i/10)%10==0)
count++ ;
else if(i%10==0)
count++ ;
}
return count ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment