long long numberof1(long long n)
{
long long count=0;
long long factor=1;
long long lower=0;
long long curr=0;
long long higher=0;
while(n/factor!=0)
{
lower=n%factor;
curr=n/factor%10;
higher=n/(factor*10);
if(curr==0)
count+=higher*factor;
else if(curr==1)
count+=lower+1+higher*factor;
else
count+=(higher+1)*factor;
factor*=10;
}
return count;
}
详细分析参见http://blog.csdn.net/sjf0115/article/details/8600599