/*==========================================================*\
| 【google面试题】给定一个正整数N,写下从1开始,到N的所有整数,
| 然后数一下其中出现的所有“1”的个数。
| (1)写一个函数f(N);
| (2)满足条件“f(N) = N”的最大的N是多少?
| 【解答】
| (1)只分析当前位是0,1或者[2,9]三种情况讨论,见sum_of_1();
| (2)找出f(n) = n的一个上界N = 10^11,N >= 10^11时,f(n) > n
| 恒成立,从10^11向下递减就算,得出答案:1 111 111 110
\*==========================================================*/
long long sum_of_1(unsigned long long n)
{
unsigned long long count = 0;
unsigned long long factor = 1;
unsigned long long lower = 0;
unsigned long long curr = 0;
unsigned long long higher = 0;
while (n / factor != 0) {
lower = n - (n / factor) * factor;
curr = (n / factor) % 10;
higher = n / (factor * 10);
switch (curr) {
case 0:
count += higher * factor;
break;
case 1:
count += higher * factor + lower + 1;
break;
default:
count += (higher + 1) * factor;
break;
}
factor *= 10;
}
return count;
}
int main()
{
unsigned long long x;
while (cin>>x)
cout<<sum_of_1(x)<<endl;
return 0;
}
【POJ-3286】a-b之间0的个数
ll self_zeros(ll x)
{
if (x == 0) return 1;
ll ans = 0;
while (x) {
if (x % 10 == 0)
++ans;
x /= 10;
}
return ans;
}
ll number_of_zeros(ll x)//小于x的所有数中0的个数
{
if (x == 0) return 0;
ll t = 10;
ll factor = 1;
ll ans = 1;
while (x / factor != 0) {
ll higher = x / (factor * 10);
ll lower = x % factor;
ll t = x / factor % 10;
switch (t) {
case 0:
ans += (higher - 1) * factor + (lower != 0 ? lower : 0);
break;
default:
ans += higher * factor;
break;
}
factor *= 10;
}
return ans;
}
int main()
{
ll a, b;
while (cin>>a>>b, (a != -1 || b != -1)) {
printf("%lld\n", number_of_zeros(b + 1) - number_of_zeros(a));
}
return 0;
}