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
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
10
179
2
Your Code
#include<iostream>
using namespace std;
#include<sstream>
int main()
{
int t;
cin>>t;
while(t--)
{ int n;
cin>>n;
int i,sum=0;
for(i=1; i<=n; i++)
{ int temp=0;
stringstream ss;
ss<<i;
string str=ss.str();
int l;
l=str.length();
int j;
for(j=l-1; j>=0; j--)
{
if(str[j]=='0')
{
temp=1;
break;
}
}
if(temp==1)
sum++;
}
cout<<sum<<endl;
}
}