This code prints the number of digits 0,1,2,... 9sbetween A and B
How does it works?
#include iostream
using namespace std;
int pot10(int i) {
int n = 1;
while (i--) n *= 10;
return n;
}
int digits(int n, int d) {
if (n 10 n d - 1) return d ? 1 : 0;
int s = 0, r = 0, i = 1;
int p = pot10(i);
while (n / (p / 10) = 1) {
if (!d) s -= 1 * (p / 10);
if ((n % p) ((d + 1) * (p / 10) - 2)) {
s += (n / p + 1) * p / 10;
r = 0;
} else {
r = (n % p)(d * (p / 10) - 2) ? (n % p)-(d * (p / 10) -
2) - 1 : 0;
s += (n / p) * (p / 10) + r;
}
i++;
p = pot10(i);
}
return s;
}
int main() {
int A, B, a, b, i;
while (cin A B, A, B) {
for (i = 0; i 9; i++) {
a = digits(A - 1, i);
b = digits(B, i);
cout b - a ;
}
a = digits(A - 1, i);
b = digits(B, i);
cout b - a endl;
}
return 0;
}
--
You received this message because you are subscribed to the Google Groups
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.