I was trying to solve a function problem in which I had to count the number of times digit 1 appeared in all non-negative integers less than n(given).
Here's my code:
int ones(int n, int d)
{
int rounddown = n - n % pow(10, d+1);
int roundup = rounddown + pow(10, d+1);
int right = n % pow(10, d);
int dig = (n/pow(10, d)) % 10;
if(dig<1)
return rounddown/10;
else if(dig==1)
return rounddown/10 + right + 1;
else
return roundup/10;
}
int countDigitOne(int n) {
int count = 0;
string s = to_string(n);
for(int i=0;i<s.length();i++)
count+=ones(n, i);
return count;
}
But the following compilation error shows up:
Line 3: invalid operands of types '__gnu_cxx::__promote_2::__type {aka double}' and 'int' to binary 'operator%'